2012-11-12 48 views
1

我正在嘗試使自定義登錄視圖彈出式視圖像一個警報視圖。我使用以下函數模擬了alertview彈出窗口。此功能在viewDidLoad中發現礦井loginViewController.m自定義登錄視圖彈出式警報視圖

-(void)initialDelayEnded { 
    self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001); 
    self.view.alpha = 1.0; 
    [UIView animateWithDuration:1.0/1.5 animations:^{ 
     self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1); 
    }completion:^(BOOL complete){ 
     [UIView animateWithDuration:1.0/2 animations:^{ 
      self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9); 
     }completion:^(BOOL complete){ 
      [UIView animateWithDuration:1.0/2 animations:^{ 
       self.view.transform = CGAffineTransformIdentity; 
      }]; 
     }]; 
    }]; 
} 
- (void)viewDidLoad 
{ 
    [self initialDelayEnded]; 
    [super viewDidLoad]; 
} 

而且我在我的firstViewController我以下列方式loginViewController調用。

LoginViewController *login = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:NULL]; 
     [self presentViewController:login animated:YES completion:NULL]; 

但它崩潰,出現以下錯誤。

'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View <UIView: 0x8674bf0; frame = (0 20; 320 460); autoresize = W+H; layer = <CALayer: 0x8670620>> is associated with <LoginViewController: 0x868a7d0>. Clear this association before associating this view with <LoginViewController: 0x8451e70>. 

有人可以幫我嗎?

在此先感謝!

+1

可能是'[super viewDidLoad]'應該先行嗎? – anticyclope

+0

沒有仍然收到相同的錯誤。 – Steaphann

回答

0

除非您有特殊原因,否則請儘早在您的本地實施中致電[super viewDidLoad];。含義:致電[self initialDelayEnded]; AFTER [super viewDidLoad];

請叫LoginViewController確保您的.xib文件只有一個File's OwnerObjects面板Placeholders和NO的ViewController對象。並確保File's Owner的自定義類是LoginViewController。您能否上傳您的.xib的屏幕截圖,具體顯示Document Outline?確定可能出錯的代碼要容易得多

+0

是的,我找到了答案。問題正是你在說的。這個問題幫助我解決它http://stackoverflow.com/questions/12434937/uiviewcontrollerhierarchyinconsistency-when-trying-to-present-a-modal-view-contr – Steaphann

+0

我很高興它幫助。我前一陣子也有同樣的問題。 – petershine

1

這裏:

[self presentViewController:login animated:YES completion:NULL]; 

你呈現viewController通過自我的猜測是viewcontroller本身。

,而不是你應該使用:

[self presentModalViewController:login animated:YES]; 

,如果你想展示你的viewcontroller,而推動它的導航堆棧。 您正在使用此代碼的類。

+0

謝謝你的回答。但它仍然崩潰,同樣的錯誤。 – Steaphann

+0

在您使用此代碼的哪個類中,錯誤指出當您調用presentViewController代碼時,與您的LoginViewController相關聯的視圖已與其他某個viewcontroller相關聯; – spider1983

+0

in firstViewcontroller.m – Steaphann

0

你檢查了你的LoginViewController xib嗎?有沒有可能將同一個視圖映射到多個視圖控制器?

+0

是的,我檢查過它,但我只用一個視圖連接了我的視圖。 – Steaphann

0

錯誤消息意味着有兩個「LogInViewController」實例。你在使用故事板嗎?如果是這樣,你可以添加視圖控制器的主要故事板文件(而不是一個單獨的筆尖),給它一個標籤/標識符(但不要連接到其他VC)。然後,您可以抓取故事板創建的實例並呈現該實例。像這樣:

//Get the stroyboard 
UIStoryBoard *mainStoryBoard = [UIStoryBoard storyboardWithName:<STORYBOARD_NAME> bundle:nil]; 

//Get the VC 
LogInViewController *login = [mainStoryBoard instantiateViewControllerWithIdentifier:<VIEWCONTROLLER_TAG>]; 

//Present 
[self presentModalViewController:login animated:YES]; 

而且,你不應該試圖在viewDidLoad:提出其他視圖控制器,它不會工作。移動代碼提出新的控制器爲viewDidAppear:

0

我解決我的應用程序登錄的問題以下列方式>

  1. 使用警報視圖中AppDelegate中展現出來的applicationDidBecomeActive
  2. 添加一個UIView控制器用XIB1來投影名爲LoginSubView。保持空白,並添加標籤到它說99
  3. 在警報框出現之前加載LoginSubView控制器。

    AFLoginViewController *LoginSubView = [[AFLoginViewController alloc] init]; 
    [_window addSubview:LoginSubView.view]; 
    [_window makeKeyAndVisible]; 
    
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is an example alert!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil]; 
    alert.alertViewStyle = UIAlertViewStyleSecureTextInput; 
    [alert show]; 
    
  4. 如果登錄成功,我只是任何其他與以下行

    for (UIView *subView in _window.subviews) 
    { 
        if (subView.tag == 99) 
        { 
         [subView removeFromSuperview]; 
        } 
    } 
    
  5. 還可以添加在同一子視圖上applicationDidEnterBackground以及之前刪除LoginSubView。以避免下次回到前臺時閃爍敏感屏幕視圖