Ios 设置代理(用于协议)仅在prepareForSegue中有效?
我发现的大多数涉及实现协议和委托的信息都涉及到这样做的步骤Ios 设置代理(用于协议)仅在prepareForSegue中有效?,ios,objective-c,delegates,protocols,Ios,Objective C,Delegates,Protocols,我发现的大多数涉及实现协议和委托的信息都涉及到这样做的步骤 DestinationViewController *destinationVC = [[destinationViewController alloc] init]; destinationVC.delegate = self; 但在经历了几个小时的挫折之后,因为我无法让它工作,我终于在prepareForSegue DestinationViewController *destinationVC = segue
DestinationViewController *destinationVC = [[destinationViewController alloc] init];
destinationVC.delegate = self;
但在经历了几个小时的挫折之后,因为我无法让它工作,我终于在prepareForSegue
DestinationViewController *destinationVC = segue.destinationViewController;
destinationVC.delegate = self;
[self performSegueWithIdentifier:@"MySegue" sender:sender];
这确实有效。我做错了什么?似乎使用第一种方法时,我的代理从未设置为self。在使用
UIStoryboard
时,有两种方法可以pushController
UIViewController *displayTable = [self.storyboard instantiateViewControllerWithIdentifier:@"nextViewcontroller"];
[self.navigationController pushViewController:displayTable animated:YES];
选项1:从情节提要
中引用实际的UIViewController
UIViewController *displayTable = [self.storyboard instantiateViewControllerWithIdentifier:@"nextViewcontroller"];
[self.navigationController pushViewController:displayTable animated:YES];
选项2:使用Segue
DestinationViewController *destinationVC = segue.destinationViewController;
destinationVC.delegate = self;
[self performSegueWithIdentifier:@"MySegue" sender:sender];
在第一种情况下,您正在分配对象并分配委托。这并不意味着在执行pushViewController
操作时,相同的引用正在传递。在这种情况下,创建了两个不同的引用。因此,您的委托是指一些其他不存在的引用
这可能会对您有所帮助。在使用
UIStoryboard
时,有两种方法可以pushController
UIViewController *displayTable = [self.storyboard instantiateViewControllerWithIdentifier:@"nextViewcontroller"];
[self.navigationController pushViewController:displayTable animated:YES];
选项1:从情节提要
中引用实际的UIViewController
UIViewController *displayTable = [self.storyboard instantiateViewControllerWithIdentifier:@"nextViewcontroller"];
[self.navigationController pushViewController:displayTable animated:YES];
选项2:使用Segue
DestinationViewController *destinationVC = segue.destinationViewController;
destinationVC.delegate = self;
[self performSegueWithIdentifier:@"MySegue" sender:sender];
在第一种情况下,您正在分配对象并分配委托。这并不意味着在执行pushViewController
操作时,相同的引用正在传递。在这种情况下,创建了两个不同的引用。因此,您的委托是指一些其他不存在的引用
希望这对您有所帮助。这是关于segues的基本教程,针对Xcode 6及更高版本进行了更新 当您使用故事板时,UIStoryboardSegue类提供了所有必要的上下文。它为您保存目标视图控制器。因此,您必须访问目标控制器抛出destinationViewController属性 如果要手动将控制器添加到导航堆栈中:
{
// binds your viewController from storyboard with local instance
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"YOUR_STORYBOARD_IDENTIFIER"];
// set your delegate
vc.delegate = self;
// push controller into navigation stack
[self.navigationController pushViewController:vc animated:YES];
}
这是关于segues的基本教程,针对Xcode 6及更高版本进行了更新 当您使用故事板时,UIStoryboardSegue类提供了所有必要的上下文。它为您保存目标视图控制器。因此,您必须访问目标控制器抛出destinationViewController属性 如果要手动将控制器添加到导航堆栈中:
{
// binds your viewController from storyboard with local instance
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"YOUR_STORYBOARD_IDENTIFIER"];
// set your delegate
vc.delegate = self;
// push controller into navigation stack
[self.navigationController pushViewController:vc animated:YES];
}
从情节提要实例化时,将调用
initWithCoder:
methid,而不是init
方法
DestinationViewController *destinationVC = [[destinationViewController alloc] init];
destinationVC.delegate = self;
当你的控制器不是来自故事板时,你是如何做的:你从代码初始化它。之后,您必须手动处理从源VC到目标VC的转换
DestinationViewController *destinationVC = segue.destinationViewController;
destinationVC.delegate = self;
是当控制器在情节提要中定义并且是序列的目标时的操作方式。
当您执行一个序列时,会调用源代码视图控制器的
prepareforsgue:
方法,在该方法中,您应该根据需要配置目标:设置属性、委托、传递数据,当从情节提要实例化时,会调用initWithCoder:
methid,而不是init
方法
DestinationViewController *destinationVC = [[destinationViewController alloc] init];
destinationVC.delegate = self;
当你的控制器不是来自故事板时,你是如何做的:你从代码初始化它。之后,您必须手动处理从源VC到目标VC的转换
DestinationViewController *destinationVC = segue.destinationViewController;
destinationVC.delegate = self;
是当控制器在情节提要中定义并且是序列的目标时的操作方式。
当您执行segue时,会调用源代码视图控制器的
prepareforsgue:
方法,在该方法中,您应该根据需要配置目标:设置属性、委托、传递数据,如果您使用故事板制作视图控制器,则在创建该类的对象时始终使用segue。您的场景我认为也发生了同样的事情,因为您使用记分板创建类,而不是重写DestinationViewController的init方法。如果您使用故事板创建视图控制器,则在创建该类的对象时始终使用segue。您的场景我认为同样的事情也发生了,因为您使用记分板生成类,而不是重写DestinationViewController的init方法。很好!归结到对象的创建位置和方式,不管有没有故事板。好的!归结到创建对象的位置和方式,有无情节提要。