简体   繁体   English

通过UINavigationController将int变量传递给UITableView

[英]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: 我试图通过UINavigationController将int变量传递给UITableView(我使用的是xcode 4.3),所以我创建了2个类(PartsTableViewController为“ UITableViewController”和PartsNavController为“ UINavigationController”),我想从当前的变量中传递变量类添加到PartsTableViewController,然后使用包含标题栏的导航控制器打开该表,因此我在当前类中编写了以下代码:

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: 在我在viewDidLoad中编写的PartsNavController类中:

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

and in PartsTableViewController I wrote in viewDidLoad: 在我在viewDidLoad中编写的PartsTableViewController中:

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

but when run, it generates the output 2 times, 但是在运行时,它会生成2次输出,

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 ???? 第一次是我发送的值,第二次是0,我只想要我发送的值,而不是0我如何才能防止这种情况并获得我发送的值?

在此处输入图片说明

I want to pass from (MaktabatyTableViewController) to (PartsTableViewController) without using segue 我想不使用segue从(MaktabatyTableViewController)传递到(PartsTableViewController)

The better way to do what you want is to push second TableViewController in existing UINavigationController. 进行所需操作的更好方法是在现有UINavigationController中推送第二个TableViewController。 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. 最简单的方法是在StoryBoard中创建NavContr,而不是在TableViews中创建,并使用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 我想不使用segue从(MaktabatyTableViewController)传递到(PartsTableViewController)

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 . 例如,您的MaktabatyTableViewController可能会向其委托发送一条消息,然后委托会将信息传递给PartsTableViewController A much more general solution is to use notifications: MaktabatyTableViewController could post a notification that PartsTableViewController listens for. 一个更通用的解决方案是使用通知: MaktabatyTableViewController可以发布一个供PartsTableViewController侦听的通知。 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. 从您的问题中可以看出,我建议使用通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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