开发者

Three20 simple navigation

I am starting to work with the Three20 library and created a UIButton using the following code:

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Add New" 
                                                                           style:UIBarButtonItemStyleBordered
                                                                          target:@"tt://samples/new"
                                                                          action:nil] autorelease];

When the user touches the button, I would like to push an instance of NewSampleViewController onto the nav. I have added the following in my app delegate's -didFinishLaunchingWithOptions method:

[map from:@"tt://samples/new" toViewController:[NewSampleViewContro开发者_运维技巧ller class]];

As it stands, when the button is touched, nothing happens at all. Nothing happens on the device and I don't see any logging going on in the console.

What have I done wrong or missed here?

Thanks!


Unless there is something magical I haven't discovered yet, I don't think you've wired your button delegation correctly. It looks like you've told the button that the string @"tt://samples/new" is the object that receives the press event and you want it to send no message (call no method / nil).

Create a method in the view controller with the button, such as this:

- (void)addButtonPressed:(id)sender{
  TTURLAction *urlAction = [TTURLAction actionWithURLPath:@"tt://samples/new"];
  [[TTNavigator navigator] openURLAction:urlAction];
}

Then replace your button init with this:

    self.navigationItem.rightBarButtonItem = 
          [[[UIBarButtonItem alloc] initWithTitle:@"Add New" 
                                            style:UIBarButtonItemStyleBordered
                                           target:self
                                           action:@selector(addButtonPressed:)]
           autorelease];

This will invoke the TTNavigator singleton instance to open the action created with the string path provided. The button needs to be handled by a button delegate and self, your view controller, is perfectly appropriate for that. The handler method then causes a three2 navigation using a path. If you've got things wired correctly in your appDelegate, three20 will create and push the view controller you mapped. Hope that helps.


The solution by Levous works, but you can simplify by using the magic Three20 openURLFromButton: selector instead of implementing your own. Try this:

self.navigationItem.rightBarButtonItem = 
[[[UIBarButtonItem alloc] initWithTitle:@"Add New" 
                                   style:UIBarButtonItemStyleBordered
                                 target:@"tt://samples/new"
                                 action:@selector(openURLFromButton:)] autorelease];

If you want to understand why this works checkout:

Three20/src/Three20UI/source/UINSStringAdditions.m.

In a nutshell, Three20 adds the openUrlFromButton method on NSString, which calls TTNavigator openURLAction.

Note: This solution will not work as the sender has to be of type UIView which UIBarButton is not. It worked before v1.02 though. Nowadays you need to implement the selector yourself with OpenURL. Jeff (Three20 big boss) made a remark on this in pull request 463: https://github.com/facebook/three20/pull/463


This might work, i have used this to push a view onto the navigation controller. Gave an action to the navigation bar button, then the action of the following:

-(IBAction)navigationRightBarButton:(id)sender{

    [[TTNavigator navigator] openURLAction:
     [TTURLAction actionWithURLPath:@"tt://samples/new"]]; // goes tot he ttLauncher class
}

that will then push the view to the correct url path hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜