0

我想重复我在单独的视图控制器中的两种方法。这是我的代码(如果代码不清楚解释情况,请添加注释):

第一个视图控制器:

-(void)GettingVariable {
    NSString *VariableGotFromSVC = [(AppDelegate *)[[UIApplication sharedApplication] delegate] VariableGotFromSVC;
    NSNumberFormatter * NsFormatterMethod= [[NSNumberFormatter alloc] init];
    [NsFormatterMethod setNumberStyle:NSNumberFormatterDecimalStyle];

    myNumberVariable = [NsFormatterMethod numberFromString:VariableGotFromSVC];

    if (!myNumberVariable) {
        Ayarlar *SecondVC = [[Ayarlar alloc]init];
        [SecondVC performSelector:@selector(touchesBegan:withEvent:)];
    }

    NSLog(@"mynumbervariable:%@", myNumberVariable);
}

第二个视图控制器:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] setVariable:VariableTextField.text];

    MekanListesi *FirstVC=[[MekanListesi alloc]init];
    [FirstVC GettingVariable];
}

该代码与该消息一起崩溃:

0x1e640b0: cmpl (%eax), %ecx

4

1 回答 1

0

首先,你不应该touchesBegan直接打电话。当在接收器上检测到触摸时调用此方法。

其次,苹果文档说明了以下touchesBegan方法:

如果您在不调用 super 的情况下覆盖此方法(一种常见的使用模式),那么您还必须覆盖用于处理触摸事件的其他方法,如果只是作为存根(空)实现。

因此,首先touchesBegan从您的代码中删除直接调用并将其移至另一个方法。

其次,如果你实施touchesBegan你应该打电话[super touchesBegan:touches withEvent:event]

编辑

如果我正确理解了您想要的内容(如果不是,请纠正我),您希望在touchesBegan选择文本字段时以及 if 语句为 false 时执行代码。所以我之前的陈述仍然有效- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event是系统提供的一个方法,当在响应者视图上检测到触摸事件时调用它,所以你不应该直接在你的代码中调用它,比如[SecondVC performSelector:@selector(touchesBegan:withEvent:)];. 如果您想在用户点击 UITextFiled 并且 if 语句为 false 时执行相同的代码,您应该将该代码提取到单独的方法中,touchesBegan并在 if 语句为 false 时调用该方法。

于 2013-05-12T13:12:40.087 回答