简体   繁体   中英

pass int variable to UITableView Through UINavigationController

I'm trying to pass int variable to UITableView through UINavigationController (I'm using xcode 4.3) So I created 2 classes (PartsTableViewController that is "UITableViewController" and PartsNavController that is "UINavigationController"), I want to pass the variable from my current class to PartsTableViewController and then open that table with its Navigation controller that contains the title bar , so I wrote in my current class the following code:

PartsNavController *partsNav = [self.storyboard instantiateViewControllerWithIdentifier:@"partsNav"];  
partsNav.groupId = myGroupp.bg_id;
[self presentModalViewController:partsNav animated:YES];

and in the PartsNavController class I wrote in viewDidLoad:

PartsTableViewController *parts = [self.storyboard instantiateViewControllerWithIdentifier:@"Parts"];
parts.groupId = groupId;
[parts.tableView reloadData];

and in PartsTableViewController I wrote in viewDidLoad:

NSLog(@"This is group: %d", groupId);

but when run, it generates the output 2 times,

This is group:1
This is group:0

first time is the value that I sent and the second time it outs 0 , I just want the value that I sent, not 0 how can I prevent this and get just the value that I sent ????

在此处输入图片说明

I want to pass from (MaktabatyTableViewController) to (PartsTableViewController) without using segue

The better way to do what you want is to push second TableViewController in existing UINavigationController. The easiest way to do that is to create that NavContr in StoryBoard and than to TableViews and connect it's cell with leading view controller with segue. And than use method below:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIViewController *destViewController = segue.destinationViewController;
    destViewController.integerValue = value;
}

在此处输入图片说明

I want to pass from (MaktabatyTableViewController) to (PartsTableViewController) without using segue

There are (at least) two strategies you can choose from:

  1. Direct communication: People often ask how to communicate between two objects, and it almost always boils down one of the objects having a reference to the other (and sometimes vice versa). To send a message to an object, you need a pointer to that object; if you've got the pointer, there's no mystery about how to communicate. Thinking about it in those terms helps you think about the issue a little differently: instead of the immediate "how do I send a message to that object?" you can instead focus on the relationship between the two objects. How was each one created? Is one of the objects the parent of the other? Is there some common parent object that can provide a pointer? How should the objects be related, if at all?

  2. Indirect communication: Sometimes instead of having two objects communicate directly, it's more appropriate to route the communication through some intermediate object. For example, your MaktabatyTableViewController might send a message to its delegate, and the delegate could then pass the information on to PartsTableViewController . A much more general solution is to use notifications: MaktabatyTableViewController could post a notification that PartsTableViewController listens for. The intermediate object in this case is the notification center. Either way, the advantage that you get with indirect communication is that neither object has to know about the other. That reduces coupling between the two classes and makes them both more flexible and more reusable.

From what I can see in your question, I'd suggest using notifications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM