Reputation: 10754
I'm using delegation to change the title of a UIButton.
.h MainView
//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 SignUp
#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];
}
So, both NSLogs Print fine, which I think means the delegate is working, however, the title on the UIButton on the Navigation Bar never changes to "Logout"
Upvotes: 0
Views: 441
Reputation: 3814
That's because you recreate STMasterViewController
(should this have been MainViewController
instead?) every time in the loggedIn
delegate method. (You can verify this by adding a breakpoint on -[MainViewController loggedIn]
and checking if self.logInOutButton
is non-nil). Instead you should get the reference to existing instance of MainViewController
and operate on that.
Upvotes: 1