"iOS Development Daily Small notes (iv)" How to remove Uialertview in iOS 7 to avoid receiving messages after delegate objects are destroyed crash

Source: Internet
Author: User

This article is a piece of my "iOS development Daily Small Notes" series, which is recorded in the development work today, can be explained with a short article or a small demo demo small tips. The classification of the article, the content of the knowledge points may be very simple, or with a short code snippet can be achieved, but in my opinion they may give the user experience, code efficiency to some improvement, or have not been in touch with the technology, very happy to learn, put here to be a little bit. In fact, 90% of the role is to help themselves review, memory, review. If crossing feel too easy, too fragmented, then there can be two options: 1, the "iOS Inquiry" classification, the article there treatise, 2, in the comments in this article mercilessly spit groove, and then turn off the page! Thank!

The students in the test group tried their best to measure our bugs. Today they found that at some point after Uialertview pop-up, do not close Uialertview directly click the Home button back to the iOS main interface, again into the program, click on the button just Uialertview, the program crash. According to my experience, it is certain that the message has been passed on to the released object, resulting in a crash.

First of all to explain the background, at present I do the project is a dozens of people at the same time online education projects, interface level relationship is also more miscellaneous. First, and do not talk about the design is reasonable, in order to synchronize the status of the State, and so on, when the program exits to the background, we will delete the current number of interfaces, when the program enters again, will get the current data and state from the server, and then re-create a number of interfaces. And Uialertview itself is not automatically disappear because you exit into the background and then go away (dismiss). So the problem is, if my Uialertview callback object (delegate) is set to a view that will be destroyed, then click the Home key program back to the background, the view is destroyed, delegate point to the memory area is the area of the release, Then enter the program, click on the Uialertview button, send the message will inevitably crash. (can refer to my demo: here Https://github.com/pigpigdaddy/ClearAlertViewDemo)

1 #import "MySubView.h"2 3 @implementationMysubview4 5- (ID) initWithFrame: (CGRect) Frame6 {7Self =[Super Initwithframe:frame];8     if(self) {9         //Initialization CodeTen          OneSelf.backgroundcolor =[Uicolor Darkgraycolor]; A          -Uialertview *alertview = [[Uialertview alloc] Initwithtitle:@"Testalert"Message@"TEST" Delegate: Self Cancelbuttontitle:@"OK"Otherbuttontitles:nil, nil]; - [Alertview show]; the     } -     returnSelf ; - } -  +- (void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) Buttonindex - { +      A } at  - @end

As you can see in my custom view, a Uialertview is created and his agent points to self.

In Appdelegate, I deleted the custom view every time I retired to the background:

1 -(void) Applicationwillresignactive: (uiapplication *) application2{ 3     Viewcontroller *rootviewcontroller = (Viewcontroller *) Self.window.rootViewController; 4     [Rootviewcontroller Removesubview]; 5 }

After you re-enter, click OK Crash, and this simulates the crash.

So think of the solution, 1, the program exits to the background when set delegate to nil, (note that OC, send a message to nil, is possible.) Not crash, and nothing will happen); 2, when the program is back to the background, manually dismiss the current uialertview.

Try the second, because the second is more reasonable, set to nil can really avoid crash, but your click can only uialertview to dismiss off, and click the selected function will be invalidated.

Before we know iOS 7, you can get to the window where Uialertview is located by UIApplication windows, and then eliminate Uialertview (see this article: http://blog.csdn.net/ u010889390/article/details/18499691 But I'm sorry I don't have the SDK for iOS 6 right now, but I did do it in iOS5 or iOS6. But iOS 7 started, you can't get to this windows, because the Uialertview show way to make a change, in short, you can not get Uialertview through uiapplication. The only thing you can do is to save a reference to the Uialertview instance where it was created, and then do other things in the right place (such as destroying the view in this example, manually dismiss off Uialertview).

Go back to the program and make this change to our custom view:

1 #import <UIKit/UIKit.h>23@interface mysubview:uiview< Uialertviewdelegate>45 @property (nonatomic, Strong) Uialertview *Alertview; 6 7 @end
1 #import "MySubView.h"2 3 @implementationMysubview4 5- (ID) initWithFrame: (CGRect) Frame6 {7Self =[Super Initwithframe:frame];8     if(self) {9         //Initialization CodeTen          OneSelf.backgroundcolor =[Uicolor Darkgraycolor]; A          - //Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Testalert" message:@ "TEST" Delegate:self CancelB uttontitle:@ "OK" otherbuttontitles:nil, nil]; - //[Alertview show]; the          -Self.alertview = [[Uialertview alloc] Initwithtitle:@"Testalert"Message@"TEST" Delegate: Self Cancelbuttontitle:@"OK"Otherbuttontitles:nil, nil]; - [Self.alertview show]; -     } +     returnSelf ; - } +  A- (void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) Buttonindex at { -      - } -  - @end

And then redefine the Removesubview method in Viewcontroller:

 1 -(void  ) Removesubview  2  { 3  mysubview *view = (Mysubview *) [ Self.view viewwithtag:1000   4   ( View) { 5  [View.alertview dismisswithclickedbuttonindex:0   Animated:yes];  6   [view Removefromsuperview];  7   8  }

After running, exit into the background, and then enter, at this time the Uialertview has been dismiss. No more users are crash by clicking on the remaining Uialertview.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.