0

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"

user1107173
  • 10,334
  • 16
  • 72
  • 117

1 Answers1

1

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.

maroux
  • 3,764
  • 3
  • 23
  • 32
  • Thanks. Yes, it's suppose to be MainViewController (sorry, I'm paranoid and change the actual variable names). How do I get a reference to the existing instance of MainViewController? – user1107173 May 03 '13 at 02:13
  • @user1107173 How is `SignUpController` created? – maroux May 03 '13 at 02:15
  • It's a segue created in Storyboard. – user1107173 May 03 '13 at 02:16
  • @user1107173 In the `prepareForSegue:sender:`, after creating `SignUpController` instance, set it's delegate. – maroux May 03 '13 at 02:20
  • What do you mean set it's delegate? Can you please provide an example? Thanks – user1107173 May 03 '13 at 02:23
  • Show me the code for `prepareForSegue:sender:` when you create sign up controller. – maroux May 03 '13 at 02:24
  • I don't have that method b/c I created a Segue with Storyboard. – user1107173 May 03 '13 at 02:26
  • @user1107173 Add `- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if (segue.identifier isEqual: @"YOUR_SEGUE_ID"){ SignUp *signUpVc = segue.destinationViewController; signUpVc.delegate = self; } }` in `MainViewController`. This (optional) method is called when a segue transition is about to happen. – maroux May 03 '13 at 02:33
  • SignUp does not have a .delegate property. – user1107173 May 03 '13 at 02:39
  • Other way around then! `self.delegate = signUpVc;` – maroux May 03 '13 at 02:40
  • I think I can only use segue.identifier when going forward. What do I use when popping from the navigation stack? – user1107173 May 03 '13 at 02:56
  • So, I tired your strategy and didn't work :( I think I did it how you suggested it. I updated the code. Thanks – user1107173 May 03 '13 at 04:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29340/discussion-between-mar0ux-and-user1107173) – maroux May 03 '13 at 04:13