2013-06-25 22 views
-1

我想顯示一個臨時警報約2秒鐘,以使用戶有機會取消操作。如果用戶點擊取消按鈕,後續的方法將不會被調用。但是,如果用戶未按取消,則將調用後續方法。任何人有任何想法如何使這項工作?在持續時間之後執行操作,如果沒有單擊alertView的buttonIndex

我有一個設置,看起來像這樣:

-(void) buttonPress { 

    //alert with message and cancel button 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"2 seconds to cancel" 
    message:@"Push Cancel to stop" delegate:self cancelButtonTitle:@"Cancel" 
    otherButtonTitles:nil, nil]; 
    alert.tag = 1; 
    [alert show]; 
    return; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == 0 && alertView.tag == 1) { 

     //action cancelled 
    } 
    else { 

     //action not cancelled 
    } 
} 

回答

5

試試這個〜2秒後關閉快訊:

-(void) buttonPress { 

    //alert with message and cancel button 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"2 seconds to cancel" 
    message:@"Push Cancel to stop" delegate:self cancelButtonTitle:@"Cancel" 
    otherButtonTitles:nil, nil]; 
    alert.tag = 1; 
    [alert show]; 

    // Create the perform selector method with alert view object and time-period 
    [self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:2]; 
    return; 
} 


// Dismiss the alert view after time-Period  
-(void)dismissAlertView:(UIAlertView *)alert 
{ 
    [alert dismissWithClickedButtonIndex:-1 animated:YES]; 

} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == 0 && alertView.tag == 1) { 

     //action cancelled 
     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(dismissAlertView:) object:alertView]; 
    } 
    else { 

     //action not cancelled 
    } 
} 
0

你可以製作一個方法,它會在2秒鐘之後調用和罷免alertview並執行您想要做任務。

您可以調用該方法如下 -

[self performSelector:@selector(yourMethodName) withObject:nil afterDelay:2]; 
+1

當取消按鈕按下取消使用執行選擇器:'[NSObject的cancelPreviousPerformRequestsWithTarget:yourTarget選擇器:aSelector對象:anArgument];' – Wain

+0

@BhaveshNai - 無2意味着2秒。 – Rohan

+0

哦對不起@Rohan –

0

設置一個NSTimer你想要的(2秒)的延遲。在計時器的委託中,用適當的參數調用UIAlertView的委託。

0

當您顯示警報(非重複)並在委託回調中將其取消時,開始NSTimer。如果定時器啓動,請關閉警報並呼叫您的後續任務。

0

試試這個:

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(popViewAfterMessage) userInfo:nil repeats:NO]; 

-(void) popViewAfterMessage { 
    // your code here... 
} 
0

下面是邏輯..

  1. 約2秒的臨時警報給用戶有機會取消操作。

    設置一個標誌變量以指示天氣繼續與否。

  2. 如果用戶點擊了取消按鈕,則隨後的方法將不被調用。 2秒,一旦UIAlertView中進來的使用者前方,並設置標誌爲假

    開始計時。

  3. 然而,如果用戶不點擊取消,則隨後的方法將被調用。

    隱藏UIAlertView中,停車2秒計時器&標誌值將是真實的,並致電後續的方法來繼續你的邏輯。

0

使用此代碼:

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(FunctionName) userInfo:nil repeats:NO]; 
-(Void)YourFunctionName 
{ 
} 
-1

使用此功能:

- (void)hideAllAlerts { 

    for (UIWindow* window in [UIApplication sharedApplication].windows) { 
     NSArray* subviews = window.subviews; 
     if ([subviews count] > 0) 
      if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]]) 
       [(UIAlertView *)[subviews objectAtIndex:0] dismissWithClickedButtonIndex:[(UIAlertView *)[subviews objectAtIndex:0] cancelButtonIndex] animated:NO]; 
    } 
} 

更改代碼:

-(void) buttonPress { 

    //alert with message and cancel button 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"2 seconds to cancel" 
    message:@"Push Cancel to stop" delegate:self cancelButtonTitle:@"Cancel" 
    otherButtonTitles:nil, nil]; 
    alert.tag = 1; 
    [alert show]; 
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(hideAllAlerts) userInfo:nil repeats:NO]; 

    return; 
} 
+0

在你的hideAllAlerts方法中有兩個錯誤... – Stas

+0

第一個是第一行的return語句。和其他 –

+0

第二個是[子視圖objectAtIndex:0]可能不是一個UIAlertView應該用於()循環而不是if() – Stas

-1
[self performSelector:@selector(sendMail) withObject:nil afterDelay:1.0]; 
0

如果顯示警報視圖後您的應用程序進入後臺這樣,即使運行。

UIBackgroundTaskIdentifier bgTask = 0; 
    UIApplication *app = [UIApplication sharedApplication]; 
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
     [app endBackgroundTask:bgTask]; 
    }]; 

    NSTimer* currentTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self 
                selector:@selector(YourMethodName) userInfo:nil repeats:YES]; 

享受!....

相關問題