2012-02-27 10 views
0

は、私は、全体のコードは次のようであるいくつかのseconds.The後UIAlertViewを却下する機能を追加します。ネットワークが利用できない場合UIAlertview EXC_BAD_ACCESS


- (void)netWorkAlert 
{ 
    UIAlertView *netWork = [[UIAlertView alloc] initWithTitle:@"error" message:@"network has problems" delegate:self cancelButtonTitle:nil otherButtonTitles: nil]; 
    [netWork show]; 
    [self performSelector:@selector(dismissAlert:) withObject:netWork afterDelay:2]; 
} 
- (void)dismissAlert:(UIAlertView *)alert 
{ 
    if(alert) 
    { 
     [alert dismissWithClickedButtonIndex:0 animated:YES]; 
     [alert release]; 
    } 
} 

netWorkAlertが呼び出されます。 netWorkAlertが2回目に呼び出されたときに

は、今私が出会った問題は、アプリが壊れていると私はdidnのXcodeは


int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([ZJAppDelegate class])); 
//Thread 1 :EXC_BAD_ACCESS(code=1,address=xc0000004) 
    } 
} 

でエラーを示し、tはARCを使用して、私はなぜかクラッシュします。私も[alert release];とコメントしていますが、2度目の問題は同じです。

誰でも私がそれを確認するのに役立つでしょうか?感謝! ありがとう!

+0

編集:なぜユーザーにアラートを拒否させるだけではないのですか?それはちょうどそれが消えて少し変だ。 – CodaFi

+0

ネットワークが利用できないとき、私はそれをユーザーに知らせたいと思う。 – scorpiozj

+1

ああ、申し訳ありませんが、他の原因によってプログラムがクラッシュします。この質問のために私は何をすべきですか? – scorpiozj

答えて

0

UIAlertViewはdismissAlert方法は(alertnilあるためにあなたのチェックがクラッシュ、このコードを防ぐことができますと呼ばれる時間によって範囲外である可能性があります。alertが外になることはありません、これを実現するためのより良い方法は、しかし、がありますスコープ

networkAlertメソッドを定義しているクラスでは、<UIAlertViewDelegate>プロトコルを実装する必要があります。以下のコードは、ユーザーが「キャンセル」ボタンをクリックしてカスタムアクションを実行することを許可します。 UIAlertView

@interface YourClassName : UIViewController <UIAlertViewDelegate> {}

@implementation YourClassName 

-(void) networkAlert 
{ 
    UIAlertView *netWork = [[UIAlertView alloc] initWithTitle:@"error" 
                 message:@"network has problems" 
                delegate:self 
              cancelButtonTitle:@"cancel" 
              otherButtonTitles:nil]; 
    [netWork show]; 
} 

- (void) alertViewCancel:(UIAlertView*)alertView 
{ 
    what ever it is you want to do when the cancel button is pressed here 
} 
1

EXC_BAD_ACCESSが解放オブジェクトにアクセスすることによって引き起こされます。これはモーダルのUIAlertViewの種類にあなたの電話をかける避けるために:

機能本体:

-(void)checkSaving 
{ 
    UIAlertView *alert = [[UIAlertView alloc] 
     initWithTitle:@"Do you want to add these results to your database?" 
     message:@"\n\n" 
     delegate:self 
     cancelButtonTitle:@"No" 
     otherButtonTitles:@"Save", nil]; 
    alert.alertViewStyle = UIAlertViewStyleDefault; 
    [alert show]; 
    //this prevent the ARC to clean up : 
    NSRunLoop *rl = [NSRunLoop currentRunLoop]; 
    NSDate *d; 
    d= (NSDate*)[d init]; 
    while ([alert isVisible]) { 
     [rl runUntilDate:d]; 
    } 

} 

あなたの選択の結果:

@interface yourViewController() 
    -(void)checkSaving 
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
//... 
@end 

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    // the user clicked one of the OK/Cancel buttons 
    if (buttonIndex == 1)//Save 
    { 
     //do something 

    } 
    if (buttonIndex == 0)//NO 
    { 
     //do something 
    } 
} 

は、インタフェース宣言の関数を登録します

お電話する: [self checkSaving];

私はこれがあなたを助けてくれることを願っています。