2013-02-24 6 views
8

Tôi có một ứng dụng xem duy nhất và muốn hiển thị một ViewController mới khi nhấn một nút thanh nav ở phía bên tay phải. Tôi gọi VC này bằng cách mã này:presentViewController hoạt hình từ bên

- (IBAction)createEntryButton:(id)sender { 
    CreateEntryViewController *vc2 = [[CreateEntryViewController alloc] init]; 
    [self presentViewController:vc2 animated:TRUE completion:nil]; 
} 

hoạt hình này, tuy nhiên, mang lại vc2 trong từ đáy mà có vẻ phản trực giác theo giao diện người dùng của tôi. Vì vậy, câu hỏi của tôi là:

Tôi làm cách nào để vc2 xuất hiện từ bên phải thay vì dưới cùng với presentViewController?

Cảm ơn.

Trả lời

9

sạch nhất sẽ được sử dụng một navigationController cho đẩy và popping quan điểm ..

nếu bạn đã có trong một NavigationController

[self.navigationCtroller pushViewController:vc2 animated:TRUE completion:nil] 

nếu bạn không phải là, điều chỉnh mã nơi điều khiển xem của bạn được thêm vào cửa sổ. Nếu VC của bạn là rootWindowController và bạn không sử dụng viết kịch bản, điều này có thể trong appdelegate bạn

nếu bạn sử dụng kịch bản, thích ứng với các kịch bản, do đó bạn đang ở trong một bộ điều khiển chuyển hướng


ELSE nếu bạn don' t muốn điều đó vì lý do nào: :) chỉ bằng tay động trong quan điểm 2. VC bằng cách sử dụng [UIView animate: vc2.view ....]

viết inline - tên phương pháp không phù hợp nhưng cho thấy cách tiếp cận chung:

UIView *v = vc2.view; 
CGRect f = v.frame; 
f.origin.x += self.view.frame.size.width; //move to right 

v.frame = f; 

[UIView animateWithDuration:0.5 animations:^{ 
    v.frame = self.view.frame; 
} completion:^(BOOL finished) { 
    [self presentViewController:vc2 animated:NO completion:nil]; 
}]; 

trong khối hoàn thành hiện tại VC2 xem điều khiển không động như bạn đã làm điều đó cho mình

+0

một bộ điều khiển chuyển hướng không cần phải hiển thị một thanh điều hướng btw :) –

+0

Bây giờ tôi gọi 'vc2' với các thông tin sau: AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate]; CreateEntryViewController * vc2 = [[CreateEntryViewController alloc] init]; [appDelegate.navController pushViewController: vc2 hoạt ảnh: TRUE]; Nhưng tôi không thể quay lại với những gì tôi nghĩ là chính xác: AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate]; [appDelegate.navController dismissViewControllerAnimated: TRUE complete: nil]; Bất kỳ suy nghĩ nào? –

+0

có, bạn không muốn gọi vc2 với appDelegate - thats .... lạ - gọi nó bằng cách sử dụng bộ điều khiển điều hướng của riêng bạn. [một trong đó chứa vc1] (vc1.navigationController) –

0

này đã giúp tôi,

- (void)presentNewViewController{ 
    NewViewController *objNewViewController =[[NewViewController alloc]initWithNibName:@"NewViewController" bundle:nil]; 

    UIView *tempNewVCView = [UIView new]; 
    tempNewVCView = objNewViewController.view; 
    tempNewVCView.frame = self.view.frame; 

    CGRect initialFrame = self.view.frame; 
    initialFrame.origin.x = self.view.frame.size.width; 

    tempNewVCView.frame = initialFrame; 

    [self.view addSubview:tempNewVCView]; 

    [UIView animateWithDuration:0.3 animations:^{ 
     tempNewVCView.frame = self.view.frame; 
    } completion:^(BOOL finished) { 
     [self presentViewController:objNewViewController animated:NO completion:^{ 
     }]; 
    }]; 
}