2012-10-10 49 views
0

我有一個視圖,如果在嘗試保存時未填充項條目的名稱字段,則彈出UIAlertView。我想顯示自己的自定義警報視圖。我用xib加載它:如何使用自定義視圖模擬UIAlertView模態行爲?

- (void)enterNameAlert { 

    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"AddNameAlertView" owner:self options:nil]; 
    enterNameAlertView = [subviewArray objectAtIndex:0]; 
    enterNameAlertView.frame = CGRectMake(232, 417, 303, 171); 
    enterNameAlertView.alpha = 0.0; 
    [self.view addSubview:enterNameAlertView]; 
    [self.view bringSubviewToFront:enterNameAlertView]; 

    //fade view in 
    [UIView animateWithDuration:.50f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void) { 

     enterNameAlertView.alpha = 1.0; 

    } completion:NULL]; 

    [UIView commitAnimations]; 

} 

但是,您仍然可以在顯示警報時單擊視圖上的所有其他元素。理想情況下,我想製作alertview模式。作爲黑客,我只是在alertview啓動時將所有其他元素設置爲xxxx.enabled = NO,並在警報上按OK時切換回xxxx.enabled = YES。

必須有一個更好的方法來取消所有觸摸self.view,但不是self.enterNameAlertView。

如果我做self.view.userInteractionEnabled = NO它取消self.view所有觸及以及self.enterNameAlertView。

任何想法?

回答

0

明白了。我使用了一個啓用了交互的UIScrollView。這會阻止自定義alertview外部的觸摸傳遞到下方的主視圖。

- (void)enterNameAlert { 

    //pop a UIScrollView behind the alert so that the user can't touch the controls behind it 
    shadowScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)]; 
    shadowScrollView.userInteractionEnabled = YES; 
    shadowScrollView.backgroundColor = [UIColor blackColor]; 
    shadowScrollView.alpha = 0.0; 
    [self.view addSubview:shadowScrollView]; 
    [self.view bringSubviewToFront:shadowScrollView]; 

    //put the alertview in front of the disabled view to make it act like a modal view 
    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"AddNameAlertView" owner:self options:nil]; 
    enterNameAlertView = [subviewArray objectAtIndex:0]; 
    enterNameAlertView.frame = CGRectMake(232, 417, 303, 171); 
    enterNameAlertView.alpha = 0.0; 
    [self.view addSubview:enterNameAlertView]; 
    [self.view bringSubviewToFront:enterNameAlertView]; 

    //animate view in 
    [UIView animateWithDuration:.50f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void) { 

     shadowScrollView.alpha = .6; 
     enterNameAlertView.alpha = 1.0; 

    } completion:NULL]; 

    [UIView commitAnimations]; 

} 
0

我覺得你的問題的解決方案是,你必須要在您的自定義警報視圖和一部開拓創新的視圖之間引入一個新的觀點,一旦警報降臨時,你需要把你的self.view並隱藏該視圖(或刪除)當你的戒備被解僱時,我已經做到了這一點,它對我來說就像魅力一樣。希望這也可以幫助你:)

+0

哇,這樣的小任務好像很多額外的繁重工作。推一個全新的viewcontroller只是爲了阻止用戶在alertview框架之外進行交互?有什麼像「觸及區域/邊界xyz =忽略」?另外,我還以爲你不能用透明背景推新視圖並看到它下面的視圖? – RyeMAC3

+0

嘿關於推視圖控制器,我不是說,我說簡單的UIView。插入那不會是一個大問題。 – Suhaiyl

+0

我已經試過了。我把1024×768的UIView主視圖和alertview之間,我仍然可以通過它觸及。哼,現在我想知道該視圖是否將userInteractionEnabled設置爲NO。這會有所作爲嗎?觸動它會通過嗎? – RyeMAC3

相關問題