我正在使用委托来更改 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 上的标题永远不会更改为“注销”