正确访问 segue 的目标视图控制器以分配协议委托
我在实现选择列表时集成 segue 和协议时遇到一些问题。
在我的选择列表 .h 中,我有:
#import <UIKit/UIKit.h>
@protocol SelectionListViewControllerDelegate <NSObject>
@required
- (void)rowChosen:(NSInteger)row;
@end
@interface SelectColor : UITableViewController <NSFetchedResultsControllerDelegate>
-(IBAction)saveSelectedColor;
@property (nonatomic, strong) id <SelectionListViewControllerDelegate> delegate;
@end
在我的选择列表 .m 中,我有:
@implementation SelectColori
@synthesize delegate;
//this method is called from a button on ui
-(IBAction)saveSelectedColore
{
[self.delegate rowChosen:[lastIndexPath row]];
[self.navigationController popViewControllerAnimated:YES];
}
我想通过从另一个表视图执行 segue 来访问此选择列表视图:
@implementation TableList
...
- (void)selectNewColor
{
SelectColor *selectController = [[SelectColor alloc] init];
selectController.delegate = (id)self;
[self.navigationController pushViewController:selectController animated:YES];
//execute segue programmatically
//[self performSegueWithIdentifier: @"SelectColorSegue" sender: self];
}
- (void)rowChosen:(NSInteger)row
{
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error Title" message:@"Error Text" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
如果我使用以下方式导航到选择列表:
[self.navigationController PushViewController:selectController 动画:YES];
将显示警报。如果我改为使用:
[self PerformSegueWithIdentifier:@“SelectColorSegue”发件人:self];
没有显示警报,因为我认为我没有将 selectController 传递到目标选择列表。请问有什么想法可以解决这个问题吗?
I'm encountering some problems in integrating segue and protocols while implementing a selection list.
In my selection list .h I have:
#import <UIKit/UIKit.h>
@protocol SelectionListViewControllerDelegate <NSObject>
@required
- (void)rowChosen:(NSInteger)row;
@end
@interface SelectColor : UITableViewController <NSFetchedResultsControllerDelegate>
-(IBAction)saveSelectedColor;
@property (nonatomic, strong) id <SelectionListViewControllerDelegate> delegate;
@end
In my selection list .m I have:
@implementation SelectColori
@synthesize delegate;
//this method is called from a button on ui
-(IBAction)saveSelectedColore
{
[self.delegate rowChosen:[lastIndexPath row]];
[self.navigationController popViewControllerAnimated:YES];
}
I would like to access to this selection list view by performing a segue from another table view:
@implementation TableList
...
- (void)selectNewColor
{
SelectColor *selectController = [[SelectColor alloc] init];
selectController.delegate = (id)self;
[self.navigationController pushViewController:selectController animated:YES];
//execute segue programmatically
//[self performSegueWithIdentifier: @"SelectColorSegue" sender: self];
}
- (void)rowChosen:(NSInteger)row
{
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error Title" message:@"Error Text" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
If I navigate to the selection list using:
[self.navigationController pushViewController:selectController animated:YES];
the alert is displayed. If I instead use:
[self performSegueWithIdentifier: @"SelectColorSegue" sender: self];
no alert is displayed, because, I think, I don't pass to the destination selection list the selectController. Any ideas to solve this issue please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当使用Segue将数据传递到destinationViewController时,您需要使用
Apple Docs中的
When using Segue to pass data to the destinationViewController you need to use the method
from the Apple Docs