我遇到了这个问题,我显示了一个警报视图,然后在按下按钮后,某些方法应该运行,但两者都不运行。
- (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];
}
没有编译错误。提前感谢!
我不知道这是否解决了您的所有问题,但在您给出的代码示例中,您可能将委托错误地设置为视图,而您希望它是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];
}