-1

我正在使用NSNoficationCenter将数组的值从一个ViewConroller 传递到另一个。

ViewControllerBViewController A的子视图

ViewController A中,我有一个将数组作为参数的方法,一旦该方法接收到数组,我将使用以下代码来存储数组的值

-(void)gettingValueOfArray:(NSArray*)newArray{

 NSDictionary *storeArray = [NSDictionary dictionaryWithObjectsAndKeys:newArray,@"newArray", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"loadArray" object:self userInfo:storeArray];

}

ViewController B我使用以下内容在viewDidLoad 中接收通知

-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadArray:) name:@"loadArray" object:nil];

}

然后用这个方法保存数组的值

-(void)saveArray:(NSNotification *)notfication{

NSArray *saveValueOfArray = [notification userinfo]objectForKey:@"newArray"];

NSLog(@"The Value of the Notification Array is : %u", saveValueOfArray.count)

}

现在我对这段代码有两个问题。

1)仅在ViewController B中的viewDidLoad被调用一次后才发送通知,第一次不起作用。

2)一旦开始工作,每次调用发送的通知都会增加1

这是第 4 次调用通知时的控制台输出,它一直在调用它所调用的所有内容。

The Value of the Array is : 10
The Value of the Array is : 10
The Value of the Array is : 10
The Value of the Array is : 10

谁能指出问题可能是什么?

有没有更简单的方法在两个控制器之间传递数组的值

4

4 回答 4

0

将 UIViewController 放在另一个 UIViewController 上似乎并不正确,而且您的代码有点“小”奇怪,但好吧……因为它是星期五,我心情很好,我将为您提供一个发布通知的小例子。

在发布 ViewController ...

-(void)gettingValueOfArray:(NSArray*)newArray{

NSDictionary *storeArray = 
[NSDictionary dictionaryWithObjectsAndKeys:newArray,@"newArray", nil];

[self postNotification:@"loadArray" withObject:storeArray];//calls postNotification

    }

-(void)sendNotification:(NSNotification *)notification{

        [[NSNotificationCenter defaultCenter] postNotification:notification];
        [notification release];

}

-(void)postNotification:(NSString *)notification withObject:(id)obj{

NSNotification *n = [NSNotification notificationWithName:notification object:obj];
[n retain];
[self performSelectorOnMainThread:@selector(sendNotification:) withObject:n waitUntilDone:NO];//calls sendNotification on the main thread        

}

在接收器视图控制器上...

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performDataLoad:)name:@"loadArray" object:nil];

}

//NOTE: if you want to receive only one notification at your ViewController B then remove the observer here

-(void)saveArray:(NSNotification *)n{

NSArray *saveValueOfArray = [[n object]objectForKey:@"newArray"];

NSLog(@"The Value of the Notification Array is : %u", saveValueOfArray.count)

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"loadArray" object:nil];


}

//Another NOTE: If you want multiples notifications then remove the observer once you are done with the class (i suppose that you in some point remove the subview) 

-(void)viewDidDisappear:(BOOL)animated{

[super viewDidDisappear:animated];

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"loadArray" object:nil];

}
于 2013-02-02T01:36:48.477 回答
0

1)只有在ViewController B中的viewDidLoad被调用一次后才发送通知,第一次不起作用

那是因为那是在通知中添加观察者的地方。如果您想更早地观察,请考虑将此代码移动到 init 方法中。

2)一旦开始工作,每次调用发送的通知都会增加1

每个添加的观察者都没有被删除。因此,这些实例将继续被观察和处理。考虑在任务结束时移除观察者。

于 2013-02-01T23:18:23.033 回答
0

this is a classic case of why you should not nest UIViewControllers. Its explained here in a much better way http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/

(Assuming you are not using the addChildViewController method to nest your uiviewcontroller B.)

Instead of having a nested viewcontroller B, just use a UIView B, and add it as a subview to view controller A's view.

You have to figure out a better way to implement viewControllerB's viewDidLoad/Appear's functionality though.

于 2013-02-01T22:54:05.413 回答
0

由于每次在视图上推送或添加视图时都会调用 viewdidLoad 函数。所以每次你的对象将被添加到通知中心并增加你在 SecondViewController 上的通知调用的计数所以放置你的添加观察者的最佳位置是 initWithnib 函数所以放置你的代码

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

initWithNibName功能上。它会按预期工作

于 2013-02-02T02:08:23.913 回答