所以我有一个有两个标签的ios应用程序。第一个标签有一个UIGestureRecognizer,它在TAPPED后调用倒计时方法。
第二个标签有一个带有三个选项的UIPickerView。
这就是我在secondTab的ViewController中的代码:
FirstViewController *firstvs = [[FirstViewController alloc] init];
NSInteger selectedRow = [self.pickerView selectedRowInComponent: 0];
if (selectedRow == 0) {
NSLog(@"Invalid Row"); // This block will not do nothing
}
else if (selectedRow == 1) {
// Call a method in firstViewController
[firstvs smallSelection];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"smallSelection2"
object:self];
}
else if (selectedRow == 2) {
[firstvs mediumSelection];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"mediumSelection3"
object:self];
}
else if (selectedRow == 3){
[firstvs largeSelection];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"largeSelection"
object:self];
}
这基本上是做什么的,如果USER选择让我们说,第2行,在同一个secondTab中,它在UILabel中显示用户选择的内容。
现在,该选择在NSNotificationCenter中广播,如每个if和else块下的代码所示。
*我为每个if语句都有3个NSnotificationCenter,我很清楚,除了我的问题之外,它是否安全。
因此,当用户选择Option 2作为第2行时,在First选项卡的ViewController中,它调用一个名为mediumSelection的方法。
在FirstViewController.m中:
-(void)mediumSelection {
// Other functions
}
但是您可能会注意到我宁愿使用NSNotificationCenter来保持firstViewController监听secondViewController,而不是仅仅执行此方法,因为我建议使用NSNotificationCenter。 请注意这个NSNotification Broadcaster在secondViewController的选择2中:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"mediumSelection3"object:self];
然后,当我从firstViewController中显示代码时,此Broadcaster会向侦听器发送一条消息:
- (id) init
{
self = [super init];
if (!self) return nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(smallSelection2:)
name:@"smallSelection2"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mediumSelection3:)
name:@"mediumSelection3"
object:nil];
return self;
}
- (void) smallSelection2:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"smallSelection2"])
NSLog (@"SmallSelection is successfully received!");
}
- (void) mediumSelection3:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"mediumSelection3"])
NSLog (@"mediumSelection is successfully received!");
}
所以这个工作,它将它记录到控制台,因为UIPicker用于滚动它接收选择。现在针对大问题以及这个NSNotification的主要原因。
在此接收器代码之前,上面我有UIGestureRecognizer和从Tap按照跟随调用的倒计时方法:
-(void) viewDidLoad {
UITapGestureRecognizer *tapScreen = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(tapScreen:)];
[self.view addGestureRecognizer: tapScreen];
}
- (void) tapScreen:(UIGestureRecognizer *)gr
{
NSLog(@"Screen has been tapped");
/*
CODE RELATED TO LABELS, NOTHING IMPORTANT
*/
timer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(countdown) userInfo: nil repeats: YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode: NSRunLoopCommonModes];
}
-(void) countdown {
/*
MORE CODE BEING EXECUTED ON THE VIEW
*/
NSLog(@"Program success: counting... %i", mainInteger);
self.hoursLeft.text = [NSString stringWithFormat:@"%i hours", mainInteger];
self.percentGauge.text = [NSString stringWithFormat:@"%1.2f", subInteger];
// FIGURE 1:
[self smallSelection];
}
现在问题是我希望根据NSNotificationCenter的选择开始倒计时。因此,如果用户从选择器中选择了第2行,则倒计时将根据“小,中,大”方法从该选择开始。
你可以看到,在倒计时方法结束时它显示“图1:”我手动调用smallSelection方法,我希望根据UIPicker选择嵌入一个If语句来检查3个可能的选择,我正在使用NSNotificationCenter,因为我正在测试这是否会与我在secondViewController上设置的实例不同 叫:[firstvs smallSelection]。
那么无论如何都要添加一个if语句来检查3个可能的选择并在点击时执行该方法吗?
答案 0 :(得分:0)
我会使用一个全局对象,其中写下所有值并从不同的viewcontrollers中更改这些值。
因此,您可以使用此全局对象从所有viewcontrollers获取值: