Xcode使用委托在控制器之间传递数据

时间:2015-05-20 12:10:08

标签: ios objective-c

我正在开发一个具有父视图的应用程序,然后使用容器嵌入其他视图,如下所示。

enter image description here

现在我只使用左侧和中央容器,这两个容器都是表视图。主视图或项目屏幕视图是我的父控制器,我希望它与两个子控制器之间传递数据,我知道最好的选择是使用委托。但是我看过的每个例子都使用了委托,创建并初始化了一个新的视图控制器,例如让左边的容器使用leftviewcontroller嵌入一个视图。每个例子都有这行代码。

import re
text ="""<tr><td valign="top">Buffer Usage / Capacity left</td>  
<td colspan=2>100 % / 0 m</td>"""
result = re.search(r"Buffer Usage.*\n.*?>(\d{1,3}) % .+",text).group(1)
print result # 100

我想我不需要创建一个新的LeftViewController,因为它已经嵌入它已经在我的子控制器列表中。所以我的问题是如何从子控制器列表中获取控制器并将父项设置为委托。我知道我是一个数组,我可以使用objectAtIndex但我怎么知道数组中项目的顺序不会改变我不能称它为标签或标识符?感谢您的任何帮助,如果问题不是我第一次设置代表那么明白。

3 个答案:

答案 0 :(得分:1)

  

我知道最好的选择是使用代表。

在这种情况下,我不会那么肯定。我认为最好的选择是拥有一个健壮的模型,并使用KVOnotifications来表示视图控制器之间的更新。

直接回答你的问题并不算太糟糕。

for (UIViewController *viewController in self.childViewControllers) {
    if ([viewController isKindOfClass:[LeftViewController class]]) {
        LeftViewController *leftViewController = (id)viewController;
        leftViewController.delegate = self;
        break;
    }
}

我认为对此的一个小改进就是使用segue。确保每个容器都有一个名为segue的容器。在此示例中,左视图控制器具有标识符为“Load Child LeftViewController”的segue。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Load Child LeftViewController"]) {
        LeftViewController *leftViewController = segue.destinationViewController;
        leftViewController.delefate = self;
    }
}

答案 1 :(得分:-1)

Its always better to use NSNotificationCenter for such complex mechanism.

*** put following code in LeftController.m ***

// *** Register a Notification to recieve a Data when something happens in Center controller ***
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receivedNotification:)
                                             name:@"hasSomeData"
                                           object:nil];

// *** create a method to receive Notification data ***
- (void)receivedNotification:(NSNotification *) notification {
    if ([[notification name] isEqualToString:@"hasSomeData"])
    {
        // do your stuff here with data
        NSLog(@"data %@",[notification object]);
    }
}

*** when something happen in center controller post a notification to inform Left Controller ***

[[NSNotificationCenter defaultCenter] postNotificationName:@"hasSomeData" object:self];

答案 2 :(得分:-1)

    //Secondvc.h
    @protocol Sendmessage<NSObject>
    @required
    -(void)Object:(NSArray *)tosend;
    @end
    @interface Secondvc:UIViewcontroller{
    id <Sendmessage> delegate;
    }
    @property(strong,nonatomic) id <Sendmessage> delegate;
    @end

    //Secondvc.m
    @implementation Secondvc
    @synthesize delegate;
    -(void)viewDidLoad{
    //Do Something here!

    }
    //Pass Some Value When a button event occured in Second vc
    -(IBAction)Send_Data{
    [self dismissViewControllerAnimated:Yes completion:nil];
    [self.delegate Object:[NSArray Arraywithobjects:@"Hello",nil]];
    }
    @end


    //FirstVc.h
    #import "Secondvc.h"
    @interface FirstVc<Sendmessage>
    @end

    //FirstVc.m
    @implementation FirstVc
    -(void)viewDidLoad{
    Secondvc* Object=[[Secondvc alloc]init];
    Object.delegate=self;
    }
    #pragma mark Secondvc Deklegate method implementation
    -(void)Object:(NSArray *)tosend{
    NSLog(@"Recieved data Form Second VC Is:\n%@",tosend);
    }
    @end

HTH!享受编码。

相关问题