gpt4 book ai didi

ios - 如何让自定义委托(delegate)工作

转载 作者:行者123 更新时间:2023-11-28 22:05:50 25 4
gpt4 key购买 nike

我正在创建自定义委托(delegate) UIAlertView 的 alert:buttonClickedAtIndex: 方法,但它无法正常工作。我正在对 UIView 进行子类化,并且我有两个标记为 01 的按钮。当我去检查我的自定义 View 的委托(delegate)时,这仍然不起作用。这是我做的代码。

自定义 View

- (void) buttonTouchedWithIdentifier:(NSInteger)identifier
{
if (identifier == 0) {
[self.delegate alert:self didClickButtonWithTagIdentifier:0];
}
if (identifier == 1) {
[self.delegate alert:self didClickButtonWithTagIdentifier:1];
}
}

* in my showInViewMethod *
[self.dismissButton addTarget:self action:@selector(buttonTouchedWithIdentifier:) forControlEvents:UIControlEventTouchDown];
[self.continueButton addTarget:self action:@selector(buttonTouchedWithIdentifier:) forControlEvents:UIControlEventTouchDown];

self.dismissButton.tag = 0;
self.continueButton.tag = 1;



* in my view controller *

nextLevelAlert = [[ARAlert alloc] init];
nextLevelAlert.delegate = self;
[nextLevelAlert showInView:self.view
withMessage:[NSString stringWithFormat:@"Congratulations, you have completed level %i.\nWould you like to continue?", levelNumber]
dismissButtonTitle:@"Menu"
continueButtonTitle:@"Next Level"];

- (void)alert:(ARAlert *)alert didClickButtonWithTagIdentifier:(NSInteger)tagId
{
if (alert == nextLevelAlert) {
if (tagId == 0) {
NSLog(@"User does not want to continue.");
}
}
}

现在,nextLevelAlert 将委托(delegate)设置为 self,并且我确实在 View Controller 的类中声明了委托(delegate)。此外,当我为 nextLevelAlert 执行 showInView... 时,它确实出现了,它正在识别正在按下的按钮。

最佳答案

我猜你的参数不是 NSInteger 而是按钮,你应该像这样更改 buttonTouchedWithIdentifier :

- (void) buttonTouchedWithIdentifier:(id)sender
{
UIButton *button = (UIButton*)sender;
NSLog(@"buttonTouchedWithIdentifier %@",@(button.tag));
if (button.tag == 0) {
[self.delegate alert:self didClickButtonWithTagIdentifier:0];
}
if (button.tag == 1) {
[self.delegate alert:self didClickButtonWithTagIdentifier:1];
}
}

另外,当比较两个对象时使用 isEqual: 而不是 ==

- (void)alert:(ARAlert *)alert didClickButtonWithTagIdentifier:(NSInteger)tagId
{
if ([alert isEqual:nextLevelAlert]) {
if (tagId == 0) {
NSLog(@"User does not want to continue.");
}
}
}

关于ios - 如何让自定义委托(delegate)工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23996649/

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