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

不羁岁月 提交于 2020-01-25 13:56:38

问题


I need to build a code 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)


回答1:


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!



来源:https://stackoverflow.com/questions/3413758/iphone-dev-need-a-id-from-the-last-view-i-visited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!