评论更新
我正在尝试使这段代码工作,但每当我按下" nextButton"时,程序结束,我无法弄清楚出了什么问题。你能看看我的代码并弄清楚是否有什么问题。
- (void)viewDidLoad {
[super viewDidLoad];
Player1.placeholder = @"Player 1";
Player2.placeholder = @"Player 2";
Notes1.placeholder = @"Notes";
Notes2.placeholder = @"Notes";
[nextButton setAction:@selector(nextButtonPressed:)];
}
-(IBAction)nextButtonPressed:(id)sender {
if ([self.delegateP respondsToSelector: @selector(addPlayerViewController:didFinishEnteringPlayer1:didFinishEneteringPlayer2:)]) {
[self.delegateP addPlayerViewController:self didFinishEnteringPlayer1:Player1.text didFinishEneteringPlayer2:Player2.text];
NSLog(@"Works");
}
[self performSelector:@selector(nextButtonPressed:) withObject:nil afterDelay:0.25];
}
我还有另外一个与此代码有关的问题。要使用代理通过不同的视图控制器传递信息,您是否必须使用按钮或条形按钮项?
正在显示的错误:
- [UIStoryboardPushSegueTemplate nextButtonPressed:]:无法识别的选择器发送到实例
答案 0 :(得分:1)
尝试在所有Objective-C异常上设置符号断点,看看是否能再次为您提供崩溃行号和错误消息。
您在条形按钮项目上设置目标/操作的代码看起来是正确的。
(我将其留作连续性,但忽略它。)
您可能尝试的一件事是将选择器更改为
:@selector(nextButtonPressed:)
(Colon添加)然后将按钮添加为操作方法的参数:
-(IBAction) nextButtonPressed: (UIButton *) sender
操作方法应该使用或不使用sender参数,但是 值得一试。
(即使您没有在IB中联系该行动,您也应该添加IBAction关键字以获得清晰度。)
你应该使用
[self presentModalViewController:gameDetails animated:YES];
(将其作为模态呈现)
或
[self.navigationController pushViewController: gameDetails animated:YES];
(将其推入导航堆栈。
但不是两者兼而有之。两者兼顾几乎肯定是导致崩溃的原因。
答案 1 :(得分:1)
首先你不能放两行:
[self presentModalViewController:gameDetails animated:YES];
[self.navigationController pushViewController: gameDetails animated:YES];
再次转到你的viewController PlayersViewController并点击故事板点击编辑器(topmenúXCode)嵌入>导航控制器现在您使用代码更改下一行:
-(void) nextButtonPressed {
NSLog(@"Hi my friend");
[self performSegueWithIdentifier:@"YOUR_SEGUE_NAME_HERE" sender:@"hi"];}
创建新功能:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:sender];
}}
ViewController“YourViewController.h:
@interface YourViewController : UIViewController
@property (nonatomic, strong) NSString *MyObjectHere;
@end