开发者

Defining an alert view and using it

开发者 https://www.devze.com 2023-03-19 10:34 出处:网络
I\'ve defined an alert view in my .h file (@property...), and in my .m file (@synthesize...) so that I can refer to it in multiple methods. When I have an alert view alloc, 开发者_开发技巧how do I tel

I've defined an alert view in my .h file (@property...), and in my .m file (@synthesize...) so that I can refer to it in multiple methods. When I have an alert view alloc, 开发者_开发技巧how do I tell it that this is the alert view that I've defined in the h and m files?


All you need to do is assign your alert view to the property you setup just like you would anything else. Like here, where myAlertProperty is the property you setup in your interface and implementation:

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

It's okay (and best) to release here since you are assigning it to a property (assuming you have retain in your @property). When you handle the dismiss you can just say:

self.myAlertProperty = nil;


Try the following. Assuming you have defined your alert view in a separate alert class:

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

In ViewController A, you call the alert class to add the alert to the current view.

[self.alertView addToView:parentViewController.view];

Then in View B, you call the alert class remove it from superview:

[self.alertView removeFromSuperviewAnimated:YES];

Make sure you are importing the alert class in both ViewController A and ViewController B

0

精彩评论

暂无评论...
验证码 换一张
取 消