【问题标题】:How many ways to pass/share data b/w view controller在视图控制器之间传递/共享数据的方式有多少
【发布时间】:2012-01-26 10:23:25
【问题描述】:

我是 IOS 和 Objective-C 以及整个 MVC 范式的新手,我坚持以下几点。

我正在开发(副本)联系人应用程序,该应用程序也可在 iphone 中作为内置应用程序使用。我想通过另一个视图控制器传递数据并且数据是传递(空):(。

我的问题是,如何将数据从一个视图传输到另一个视图?

【问题讨论】:

    标签: iphone objective-c xcode xcode4.2 data-sharing


    【解决方案1】:

    正如您得到的大多数答案一样,在一个控制器和另一个控制器之间传递数据只是意味着将变量从一个控制器分配给另一个控制器。 如果您有一个控制器来列出您的联系人,另一个控制器来显示联系人详细信息,并且流程从列表开始并在选择联系人后进入详细信息,您可以分配联系人变量(可能是数组中的一个对象,即显示在您的列表中)并在显示此之前将其分配给详细视图控制器。

    - (void)goToDetailViewControllerForContact:(Contact *)c
    {
        ContactDetailViewController *detailVC = [[[ContactDetailViewController alloc] init] autorelease];
        detailVC.contact = c;
        [self.navigationController pushViewController:c animated:YES];
        //[self presentModalViewController:detailVC animated:YES]; //in case you don't have a navigation controller
    }
    

    另一方面,如果你想从细节控制器插入一个新的联系人到列表控制器,我想最好的方法是将列表控制器分配给细节控制器,所以当一个联系人是添加了委托通知并按预期行事(将联系人插入数组并重新加载表格视图?)。

    @protocol ContactDelegate <NSObject>
    - (void)contactWasCreated:(Contact *)c;
    // - (void)contactWasDeleted:(Contact *)c; //may be useful too...
    @end
    
    @interface ContactListViewController : UIViewController <ContactDelegate>
    @property (nonatomic, retain) NSArray *contacts;
    ...
    @end
    
    @implementation ContactListViewController
    @synthesize contacts;
    ...
    
    - (void)goToDetailViewControllerForContact:(Contact *)c
    {
        ContactDetailViewController *detailVC = [[[ContactDetailViewController alloc] init] autorelease];
        detailVC.contact = c;
        detailVC.delegate = self;
        [self.navigationController pushViewController:c animated:YES];
        //[self presentModalViewController:detailVC animated:YES]; //in case you don't have a navigation controller
        }
    
    - (void)contactWasCreated:(Contact *)c
    {
        self.contacts = [self.contacts arrayByAddingObject:c]; //I'm not sure this is the correct method signature...
        [self reloadContacts]; //may be [self.tableView reloadData];
    
    }
    
    ...
    @end
    
    
    @interface ContactDetailViewController : UIViewController
    
    @property (nonatomic, assign) id<ContactDelegate> delegate;
    ...
    @end
    
    
    @implementation ContactDetailViewController
    @synthesize delegate; //remember to don't release it on dealloc as it is an assigned property
    ...
    
    - (void)createContactAction
    {
        Contact *c = [[[Contact alloc] init] autorelease];
        [c configure];
        [self.delegate contactWasCreated:c];
    }
    
    ...
    @end
    

    【讨论】:

      【解决方案2】:

      从技术上讲,你不应该!
      整个想法不是让“视图”来控制数据发生的事情。

      您想要做的是在控制器之间传递数据(我想这正是您打算做的事情)。

      您可以使用共享模型(两个视图控制器都可以访问的对象的实例)来保存您想要共享的数据,

      您可以使用通知来传递数据(它最适合某些情况)。

      您可以将某些内容写入磁盘并稍后再次读取。

      您可以使用 NSUserDefaults。

      您可以使用钥匙串。

      ...

      【讨论】:

        【解决方案3】:

        最好的办法是:

        • 在第二个视图控制器中声明适当的@property
        • 创建时,只需使用
        • 设置属性

        viewController.property = valueYouWantToPass;

        【讨论】:

          【解决方案4】:

          我是代表和协议的忠实粉丝。
          并且在某些情况下使用单例模式。

          【讨论】:

            【解决方案5】:

            在视图控制器之间传递/共享数据的两种方式

            创建一个对象并像这样发送数据

            QGraduteYr *tableverify=[[QGraduteYr alloc]initWithStyle:UITableViewStyleGrouped];
                tableverify.mystring=myString
                [self.navigationController pushViewController:tableverify animated:YES];
            

            另一种方法是将其存储在委托中并通过共享委托使用它

            MedicalAppDelegate *appdelegate=(MedicalAppDelegate *)[[UIApplication sharedApplication]delegate];
                appdelegate.collnameStr=collStr;
            

            并且在任何你需要的地方使用这个应用程序代表价值

            【讨论】:

            • 您回答中的第二种方法确实很糟糕。不要像使用全局变量一样使用应用程序委托。如果您这样做,您的代码需要重新考虑。
            • 感谢提高我的标准
            • 乐于助人:)。请在此处查看我的回答stackoverflow.com/a/8730326/123632 了解更多详情。
            猜你喜欢
            • 2017-06-06
            • 1970-01-01
            • 2011-07-09
            • 2013-06-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多