按下 UIAlerView 按钮后方法没有响应

发布于 2024-12-29 07:33:41 字数 2584 浏览 0 评论 0原文

我遇到这个问题,我显示警报视图,然后按下按钮后某些方法应该运行,但两者都不会。

- (void)insertNewObject //Works fine
{
//AlertView is a subclass of UIAlertView I created.
    AlertView *al = [AlertView alloc];
    al = [al initWithTitle:@"title" message:@"message" delegate:[self view] cancelButtonTitle:@"cencel" okButtonTitle:@"ok"];
    [al.titlebox becomeFirstResponder];

    //[view setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [al show];
} /Till here everything works fine.

下面的所有代码都不起作用。或者它们起作用了,我只是不知道,因为它们不运行。

//This method does no run al all.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex2
{

    if(buttonIndex2 == 0)
    {
        _buttonIndex = buttonIndex2;
        [self showAbortAlert];

    } else 
    {
    _buttonIndex = buttonIndex2;
        [self addObject];
    }
}

- (void)addObject 
{
    if(_buttonIndex == 1) {
        // Create a new instance of the entity managed by the fetched results controller.
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    // If appropriate, configure the new managed object.
    // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
    [newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
    //[newManagedObject setValue:<#(id)#> forKey:<#(NSString *)#>];
    //[newManagedObject setValue:<#(id)#> forKey:<#(NSString *)#>];

    // Save the context.
        NSError *error = nil;
        if (![context save:&error]) {
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    } else {
        [self showAbortAlert];
    }
}

- (void)showAbortAlert
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Action Aborted" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert setAlertViewStyle:UIAlertViewStyleDefault];
    [alert show];
}

没有编译错误。 提前致谢!

I have this problem where I show an alert view and then after a button is pressed certain methods should run but neither would.

- (void)insertNewObject //Works fine
{
//AlertView is a subclass of UIAlertView I created.
    AlertView *al = [AlertView alloc];
    al = [al initWithTitle:@"title" message:@"message" delegate:[self view] cancelButtonTitle:@"cencel" okButtonTitle:@"ok"];
    [al.titlebox becomeFirstResponder];

    //[view setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [al show];
} /Till here everything works fine.

All the code below here dont work. Or they work, I just dont know coz they dont run.

//This method does no run al all.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex2
{

    if(buttonIndex2 == 0)
    {
        _buttonIndex = buttonIndex2;
        [self showAbortAlert];

    } else 
    {
    _buttonIndex = buttonIndex2;
        [self addObject];
    }
}

- (void)addObject 
{
    if(_buttonIndex == 1) {
        // Create a new instance of the entity managed by the fetched results controller.
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    // If appropriate, configure the new managed object.
    // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
    [newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
    //[newManagedObject setValue:<#(id)#> forKey:<#(NSString *)#>];
    //[newManagedObject setValue:<#(id)#> forKey:<#(NSString *)#>];

    // Save the context.
        NSError *error = nil;
        if (![context save:&error]) {
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    } else {
        [self showAbortAlert];
    }
}

- (void)showAbortAlert
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Action Aborted" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert setAlertViewStyle:UIAlertViewStyleDefault];
    [alert show];
}

No compilation errors.
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

双手揣兜 2025-01-05 07:33:41

我不知道这是否可以解决您所有的问题,但是在您给出的代码示例中,当您希望它成为 ViewController 时,您可能会错误地将委托设置为视图:

也就是说,而不是:

al = [al initWithTitle:@"title" message:@"message" delegate:[self view] cancelButtonTitle:@"cencel" okButtonTitle:@"ok"];

您可能想要这样做:

al = [al initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cencel" okButtonTitle:@"ok"];

另外,由于您分配了警报,所以不要忘记在调用 show 后释放它。

//some code here

[al show];
[al release];
}

I don't know if this fixes all your problems, but in the code example you gave, you are probably setting the delegate falsely to the view, when you want it to be the ViewController:

That is, instead of:

al = [al initWithTitle:@"title" message:@"message" delegate:[self view] cancelButtonTitle:@"cencel" okButtonTitle:@"ok"];

you probably want to do this:

al = [al initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cencel" okButtonTitle:@"ok"];

Also, since you alloc'ed the alert, don't forget to release it after you've called show.

//some code here

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