2012-01-19 31 views
15

Tôi có nhiều UIViewControllers trong Bảng phân cảnh ứng dụng iOS của tôi. Tôi muốn mở một trong những UIViewControllers (trong storyboard) từ một UIViewController khác nhau.Bảng điều khiển hiện tại ViewController từ một ViewController khác

Tôi đã thử mã bên dưới, nhưng nó không hoạt động mặc dù tôi đã sử dụng nó trước iOS 5 và nó hoạt động tốt.

- (IBAction)addNotes:(id)sender { 
    NotesViewController *notesView = [[NotesViewController alloc] initWithNibName:@"NotesViewController" bundle:nil]; 
    [self presentModalViewController:notesView animated:YES]; 
} 

Tôi có thể thực hiện hành động này với bảng phân cảnh iOS 5 bằng cách nào?

Trả lời

48

Giả sử bạn có kịch bản, hãy để kịch bản và cung cấp cho VC của bạn một định danh (Thanh tra), sau đó làm:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER"]; 
[self.navigationController pushViewController:vc animated:YES]; 

Giả sử bạn có một file xib bạn muốn làm:

UIViewController *vc = [[UIViewController alloc] initWithNibName:@"NIBNAME" bundle:nil]; 
[self.navigationController pushViewController:vc animated:YES]; 

Không có tệp xib:

UIViewController *vc = [[UIViewController alloc] init]; 
[self.navigationController pushViewController:vc animated:YES]; 
+0

hey cảm ơn darwin, cho trả lời của bạn ... nhưng xib là pres ent trong storyboard? – Ranjit

+0

Hiểu sai câu hỏi của bạn. Đã cập nhật câu trả lời của tôi. Làm cách tiếp cận 1). – DAS

+0

hey tôi đã kiểm tra với cách tiếp cận đầu tiên nhưng không may mắn, Darwin, nó bị rơi – Ranjit

1
UIViewController *initialHelpView = [[UIStoryboard storyboardWithName:@"StoryBoard_IDentifier" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController_Identifier"]; 
[self presentViewController:initialHelpView animated:YES completion:nil]; 
4

Làm theo sau sẽ hoạt động trên Swift 3.0 trở lên.

Storyboard

let storyBoard = UIStoryboard(name: "Main", bundle: nil) 
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "Identifier") 
self.navigationController?.pushViewController(mainViewController, animated: true) 

.xib

let viewController = UIViewController(nibName: "NibName", bundle: nil) 
    self.navigationController?.pushViewController(viewController, animated: true) 

Without .xib

let viewController = UIViewController() 
self.navigationController?.pushViewController(viewController, animated: true) 
1

Cập nhật với các phiên bản Swift 3.1

nếu bạn có không nhúng menu điều khiển sau đó

let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2") 
self.present(viewController2, animated: true, completion: nil) 

và, nếu bạn nhúng menu điều khiển sau đó

let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2") 
self.navigationController?.pushViewController(viewController2, animated: true) 
Các vấn đề liên quan