views:

35

answers:

1

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  A: 

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!

Neal L
Yes, its right its like that, :0) but when i do it, my debugger send my a error when i try to use retain protetry... :/
NeoNmaN
Without seeing the code it would be kinda hard to debug the error. ;)
Neal L
Don't use "retain"; NSUInteger isn't an Objective-C object type. Use "assign" instead.
Seamus Campbell
Thanks Seamus - my oversight there. I've updated the code sample to fix this.
Neal L