gpt4 book ai didi

ios - 委托(delegate)内的 Obj-C 委托(delegate)

转载 作者:行者123 更新时间:2023-12-01 17:39:52 28 4
gpt4 key购买 nike

我已经将我用作标题 View 的 View 子类化,它里面有一些按钮委托(delegate),它工作得很好。

但是,我在 viewController 上方展示了一个 modalViewController 。
(在我的 modalViewController 中,我实现了相同的 header ,并且它确实从 header 中获取了委托(delegate))但是如果按下 header 的后退按钮,它自己必须委托(delegate)给前一个 viewController。

我做了同样的功能,但我的 viewController 从来没有得到它的委托(delegate)...... :(
我对 Obj-C 很陌生,我不知道我在这里做的事情可能是非法的。

这是 modalViewController 尝试委托(delegate)给前一个 viewController 的代码

#pragma mark - header delegate
- (void)header:(header *)header backbuttonPressed:(UIButton *)sender
{
if(header == logo)
{
NSLog(@"gotBackButtonDelegate");
//delete the items array
//_itemSourceArray = nil;
[delegate allEventsDrillPage:self backbuttonPressed:sender];
[self dismissViewControllerAnimated:YES completion:nil];
}
}

这个委托(delegate)确实从标题 View 中触发。

但是在我之前的 viewController 中:
- (void)allEventsDrillPage:(allEventsDrillPage *)allEventsDrillPage backbuttonPressed:(UIButton *)sender //doesn't work :(
{
NSLog(@"got back delegate!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// [self dismissViewControllerAnimated:YES completion:nil];
_drillPage = nil;
}

永远不会被叫

我这样调用modalViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_allEventsTableView deselectRowAtIndexPath:indexPath animated:YES];
[self->_allEventsTableView setNeedsDisplay];
[self->_allEventsTableView reloadData];
_drillPage = [[allEventsDrillPage alloc]initWithDictionary:((NSDictionary*) [_tableDataSource objectAtIndex:indexPath.row])];
_drillPage.delegate = self;
[self presentViewController:_drillPage animated:YES completion:nil];
}

在它的 .h 文件中我做了
@interface allEvents : UIViewController <headerDelegate , UITableViewDataSource , UITableViewDelegate ,allEventsDrillPageDelegate>

我不明白我在这里错过了什么:-/有人可以看看吗?
如果需要更多信息,我会添加只是要求它。

编辑:

modalViewController 中委托(delegate)的协议(protocol)
@class allEventsDrillPage;
@protocol allEventsDrillPageDelegate //define delegate protocol
- (void)allEventsDrillPage:(allEventsDrillPage*)allEventsDrillPage backbuttonPressed:(UIButton*)sender;
@end
@interface allEventsDrillPage : UIViewController
{
id<allEventsDrillPageDelegate> __weak delegate;
....
}
@property (weak, nonatomic) id <allEventsDrillPageDelegate> delegate; //define

最佳答案

咱们试试吧:

- (void)header:(header *)header backbuttonPressed:(UIButton *)sender
{
if(header == logo)
{
NSLog(@"gotBackButtonDelegate");
// delete the items array
//_itemSourceArray = nil;

// my comment: you should replace "delegate" to "_delegate"
// and it works. I tested. It's OK. Wow.

[_delegate allEventsDrillPage:self backbuttonPressed:sender];
[self dismissViewControllerAnimated:YES completion:nil];
}
}

关于ios - 委托(delegate)内的 Obj-C 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23310786/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com