按下UIAlertView按钮后没有响应的方法

时间:2012-01-27 10:22:42

标签: objective-c methods uialertview instance-variables

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

- (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];
}

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

1 个答案:

答案 0 :(得分:2)

我不知道这是否可以解决您的所有问题,但是在您给出的代码示例中,当您希望它成为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];
}