涉及多个视图控制器,如何将值发送回起始视图控制器?

时间:2014-11-20 04:54:14

标签: ios objective-c ios7

我对iOS很新,并且对我在网上找到的资源不知所措。我的用例很简单

a。)ViewController parent具有名为categoryLabel的标签。点击标签category即可打开新视图View 1
b。)View 1,显示所有组。让我们说A, B, C。这将显示在表格上 c。)当用户点击任何群组A, B or C时,系统会显示包含该群组中所有类别的新视图View 2。例如,用户点击AView 2用户会看到类别A1, A2, A3。 d。)现在,当用户点击任何特定类别时,它如何返回到ViewController parent并分配给categoryLabel

我不知道采用这种设计的最佳方法是什么。 非常感谢任何指导

2 个答案:

答案 0 :(得分:1)

希望这会有所帮助

让我们举一个例子,你是从A - > B要从B向A发送一些数据,有很多技术要做,但使用委托方法和块更好的方法。

委托方式: -

在您的B.h文件中

@protocol yourDelegate <NSObject>

-(void)whichCategoryClicked:(NSString *)categoryName;

@end

@interface B : UIView
@property(nonatomic, assign)id<yourDelegate> delegate;

B.m

在单击特定类别后调用此委托方法。

[self.delegate whichCategoryClicked :@"Category_name"];

A.h

将其指定为委托并导入上述类

@interface A.h : UIViewController<yourDelegate>

并在A.m

中实施此方法

首先在viewdidload中

{


 B *objB = [[B alloc]init];
    objB.delegate = self;

}

-(void)whichCategoryClicked:(NSString *)categoryName
{
categoryLabel.text = categoryName;
}

答案 1 :(得分:0)

您可以将此用途的本地通知名称用作iOS中的NSNotificationCenter。其工作原理如下:

要发送来自您所在视图的通知,并希望从该视图中发送一些值,请使用以下代码:

NSDictionary *dict;
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationKey" object:nil userInfo:dict];

现在在任何一个视图控制器上,你可以在该类的viewDidLoad上添加观察者:

[[NSNotificationCenter defaultCenter] addObserver:self
                                                selector:@selector(methodToCall:)
                                                 name:@"NotificationKey"
                                               object:nil];

现在调用上面一行写的方法:

- (void)updateImageFromArray:(NSNotification *)notification {
    // your dict
    NSDictionary *dictUserInfo = [notification userInfo];

}
相关问题