定义警报视图并使用它

时间:2011-07-10 23:12:25

标签: ios iphone uialertview

我在我的.h文件(@property ...)和我的.m文件(@synthesize ...)中定义了一个警报视图,以便我可以在多种方法中引用它。当我有一个警报视图alloc时,如何告诉它这是我在h和m文件中定义的警报视图?

2 个答案:

答案 0 :(得分:0)

尝试以下方法。假设您已在单独的警报类中定义了警报视图:

- (void)addToView:(UIView *)view
    // Adds itself as a subview to the specified view.
{
    [self addToView:view animated:NO];
}

在ViewController A中,您调用alert类将警报添加到当前视图。

[self.alertView addToView:parentViewController.view];

然后在视图B中,您调用警报类将其从superview中删除:

[self.alertView removeFromSuperviewAnimated:YES];

确保在ViewController A和ViewController B中导入警报类

答案 1 :(得分:0)

您需要做的就是将警报视图分配给您设置的属性,就像您想要的一样。就像这里一样,myAlertProperty是你在界面和实现中设置的属性:

UIAlertView *newAlert = [[UIAlertView alloc] initWithTitle:@"SomeTitle" message:@"SomeMessage" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
self.myAlertProperty = newAlert;
[newAlert show];
[newAlert release];

由于您将其分配给属性(假设您的@property中有retain),因此可以(并且最好)在此处发布。当你处理解雇时,你可以说:

self.myAlertProperty = nil;