Doing a push segue using a custom UIBarButton

I have this code that lets me create a custom UIBarButton which is inside my viewDidLoad.

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                               style:UIBarButtonItemStyleDone
                                                              target:nil
                                                              action:nil];
    self.navigationItem.rightBarButtonItem = doneButton;

Now, I want this method below to be called when the UIBarButton is pressed so that a segue to the next view controller will be done.

- (void) didPressDone {

    PointsResultsViewController *pointsResults = [self.storyboard instantiateViewControllerWithIdentifier:@"resultsPointsVC"];

    [self.navigationController pushViewController:pointsResults animated:YES];
}

I've done some research regarding this. However the ones I saw were all about dragging a UIBarButton to the ViewController from the Object Library then just wire it up to the next VC, have the method prepareForSegue, done, simple as that. In my case, it's quite different.

Advice please :)

Answers


You can set the target to self and the action to your didPressDone selector:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                               style:UIBarButtonItemStyleDone
                                                              target:self
                                                              action:@selector(didPressDone:)];

The selector should have a signature like this:

- (void)didPressDone:(id)sender

How about, this-

Step 1:

In your Viewcontroller.h file, just create an IOBOutlet of your UIBarButton like this-

@property(nonatomic, strong) IBOutlet UIBarButton *doneButton;

and an IBAction method for it-

-(IBAction)didPressDone:(id)sender;

Step 2:

Wire up the UIBarButton through the storyboard and also attach the action method with it.

Step 3:

Now let's go to the Viewcontroller.m file and in the -(void)viewDidLoad method, initialize the UIBarButton the way, you wanted-

self.doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                               style:UIBarButtonItemStyleDone
                                                              target:nil
                                                              action:nil];
    self.navigationItem.rightBarButtonItem = self.doneButton;

Other than the method signature, the "didPressDone" method implementation will be same-

-(IBAction)didPressDone:(id)sender {

    PointsResultsViewController *pointsResults = [self.storyboard instantiateViewControllerWithIdentifier:@"resultsPointsVC"];

    [self.navigationController pushViewController:pointsResults animated:YES];
}

Hope, this could help.


Need Your Help

Configure port for BlazeDS

flex tomcat blazeds

I'm new to BlazeDS. I've downloaded BlazeDS 4 and installed in on my Tomcat server. How can I make it to listen to port distinct from 8080? I change port in channels configuration in services-confi...

catch null object in js

javascript html css

How can i catch the object is null or not an object. Actually i have wrote this line in if condition for solving it, but not working.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.