正确访问 segue 的目标视图控制器以分配协议委托

发布于 2024-12-21 08:15:54 字数 1716 浏览 1 评论 0原文

我在实现选择列表时集成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花期渐远 2024-12-28 08:15:54

当使用Segue将数据传递到destinationViewController时,您需要使用

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SelectColorSegue"]) {
        SelectColor *vc = segue.destinationViewController;
        vc.delegate = self;
    }
}

Apple Docs中的

方法

此方法的默认实现不执行任何操作。子类可以
覆盖它并使用它来将任何相关数据传递到视图
即将显示的控制器。 segue 对象包含
指向两个视图控制器以及其他信息的指针。

When using Segue to pass data to the destinationViewController you need to use the method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SelectColorSegue"]) {
        SelectColor *vc = segue.destinationViewController;
        vc.delegate = self;
    }
}

from the Apple Docs

The default implementation of this method does nothing. Subclasses can
override it and use it to pass any relevant data to the view
controller that is about to be displayed. The segue object contains
pointers to both view controllers among other information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文