开发者

iPhone Dev - need a id from the last view i visited

I need to build a c开发者_开发问答ode to track what id i use from an order view, but now i can't get it to work, can somebody paste a sample code to me?

i need a TagID from view1 -> view2, so when i'm landing on view2 i can get informations about it and send to users screen.

i hove on a little help here :0)


I think what you're saying here is that you are moving from one UIView to another in your application, and you need some way of "passing" a variable from view1 to view2.

This is a common use-case with iPhone app design, and there are a few approaches. Here is what I think is the easiest approach, and it will work for any object at all (integers, NSManagedObjects, whatever): create an iVar in your second view and set it to the value you want to track before you make it visible.

Set this up in your ViewTwoController with something like:

ViewTwoController.h:
====================
@interface ViewTwoController : UIViewController {
    NSUInteger *tagID;
}
@property (nonatomic, assign) NSUInteger *tagID;
@end

ViewTwoController.m
===================
@synthesize tagID;

So at this point your ViewTwoController has an iVar for tagID. Now all we have to do from View One is create the ViewTwoController, assign a value to tagID, then display that second view. This could be done in your button press selector, or from a UITableView row as such:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ViewTwoController *viewTwoController = [[ViewTwoController alloc] init];
    viewTwoController.tagID = self.tagID;  // !! Here is where you "pass" the value
    [self.navigationController pushViewController:viewTwoController animated:YES];
}

What the code above does is: (1) create a new ViewTwoController, (2) assign the value of the tagID to the tagID iVar in ViewTwoController, then (3) present view two to the user. So in your ViewTwoController code, you can access the tagID with self.tagID.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜