0

我正在使用委托来更改 UIButton 的标题。

.h 主视图

//MainViewController.h
#import <UIKit/UIKit.h>

@class SignUpDelegate;
@protocol SignUpDelegate <NSObject>
@required
-(void)loggedIn;
@end

@interface MainViewController : UITableViewController <NSFetchedResultsControllerDelegate>
{
     id <SignUpDelegate> delegate;
}
@property (nonatomic, assign) id <SignUpDelegate> delegate;
-(void)loggedIn;

@end

.m

@interface MainViewController ()
//This button is connected to the UINavigationBar Button that needs its title changed.
//Via Interface Builder, the default value of the title is setup as "Login"
-@property (weak, nonatomic) IBOutlet UIBarButtonItem *logInOutButton;
@end    

-(void)loggedIn
{
    NSLog (@"This is Logged in inside MainView.m");
    self.logInOutButton.title = @"Logout";
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *destinationViewController = segue.destinationViewController;
    Signup *signUp = [destinationViewController isKindOfClass:[Signup class]] ? (Signup*)destinationViewController : nil;
    signUp.mainViewController = self.delegate;
}

.h 注册

#import <UIKit/UIKit.h>
#import "MainViewController.h"
@interface SignUp : UIViewController <UITextFieldDelegate, UIActionSheetDelegate, SignUpDelegate>
@property (strong, nonatomic) MainViewController *mainViewController;
@end

.m

@synthesize mainViewController;
- (IBAction)createUser:(id)sender
{
   [self loggedIn];
}

- (void) loggedIn
{
    NSLog (@"This is Logged in inside SignUp");
    [mainViewController loggedIn];
}

因此,两个 NSLog 打印都很好,我认为这意味着委托正在工作,但是,导航栏上 UIButton 上的标题永远不会更改为“注销”

4

1 回答 1

1

那是因为您每次都在委托方法中重新创建STMasterViewController(应该改为吗?)。(您可以通过添加断点并检查是否为非零来验证这一点)。相反,您应该获取对现有实例的引用并对其进行操作。MainViewControllerloggedIn-[MainViewController loggedIn]self.logInOutButtonMainViewController

于 2013-05-03T02:02:24.217 回答