2015-03-01 19 views
29

Tôi đã cố gắng làm lại công việc trên ứng dụng của tôi theo chương trình. (Nếu không sử dụng bảng phân cảnh)Tạo một navigationController lập trình (Swift)

Tôi sắp hoàn tất, ngoại trừ việc thực hiện điều khiển điều hướng theo cách thủ công.

Tôi đã thực hiện một số nghiên cứu nhưng tôi không thể tìm thấy bất kỳ tài liệu nào về việc triển khai thủ công điều này. (Tôi bắt đầu tạo ứng dụng dưới dạng một ứng dụng xem đơn)

Hiện tại, tôi chỉ có 1 chế độ xem. Và tất nhiên ứng dụngDelegate

Trình điều khiển điều hướng, sẽ được sử dụng trong tất cả các trang trong ứng dụng.

Nếu có ai có thể giúp tôi hoặc gửi liên kết tới một số tài liệu thích hợp để thực hiện chương trình này, nó sẽ được đánh giá cao.

EDIT:

Tôi quên đề cập đến nó trong Swift.

+1

thể trùng lặp của [Programatically tạo UINavigationController trong iOS] (http://stackoverflow.com/questions/22981610/programatically-creating-uinavigationcontroller-in-ios) –

+0

Lưu ý rằng trong đại đa số các các trường hợp, bạn chỉ có thể làm điều đó trên bảng phân cảnh ... https://stackoverflow.com/a/22981726/294884 Sau đó bạn có thể tắt thanh nút bất ngờ và nó "tốt như" khi thực hiện theo chương trình. – Fattie

Trả lời

55

Swift 1, 2:

self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 
var nav1 = UINavigationController() 
var mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller 
nav1.viewControllers = [mainView] 
self.window!.rootViewController = nav1 
self.window?.makeKeyAndVisible() 

Swift 3+:

self.window = UIWindow(frame: UIScreen.main.bounds) 
let nav1 = UINavigationController() 
let mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller 
nav1.viewControllers = [mainView] 
self.window!.rootViewController = nav1 
self.window?.makeKeyAndVisible() 
+1

Nevermind Tôi nhận ra nơi tôi đã đi sai. Thay đổi nó trở lại mainView và đã làm "var mainView = NameOfYourViewController()" Và nó đã làm việc. Cảm ơn bạn rất nhiều vì đã giúp đỡ! – MLyck

+0

Nếu tôi muốn thêm nó vào chế độ xem đã có uitabbarcontroller thì sao? – TomSawyer

+0

@TomSawyer, Trước khi thêm bộ điều khiển trên tabbar, tạo đối tượng UINavigationController sử dụng bộ điều khiển của bạn và sử dụng đối tượng điều hướng để thêm vào UITabbarcontroller –

0

Hãy thử cái này. Nó sẽ hướng dẫn bạn cách sử dụng bộ điều khiển điều hướng.

Programatically creating UINavigationController in iOS

AppDelegate.h

#import <UIKit/UIKit.h> 
    #import "LoginViewController.h" 

    @interface AppDelegate : UIResponder <UIApplicationDelegate> 

    @property (strong, nonatomic) UIWindow *window; 
    @property (strong,nonatomic) UINavigationController *navigationController; 
    @property (strong,nonatomic) LoginViewController *loginVC; 

    @end 

AppDelegate.m

#import "AppDelegate.h" 
    #import "LoginViewController.h" 

    @implementation AppDelegate 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 

    self.loginVC = [[LoginViewController alloc]initWithNibName:nil bundle:nil]; 
    self.loginVC.title = @"Login Page"; 

    self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.loginVC]; 

    self.window.rootViewController = self.navigationController; 
    [self.window makeKeyAndVisible]; 
    } 

Sau đó, khi bạn muốn đẩy bộ điều khiển xem khác, sử dụng đơn giản mã sau đây để di chuyển đến một bộ điều khiển xem .

- (IBAction)pushMyProfileView:(id)sender 
{ 
    self.myProfileVC = [[MyProfileViewController alloc]initWithNibName:nil bundle:nil]; 
    [appDelegate.navigationController pushViewController:self.myProfileVC animated:YES]; 
} 
+0

Xin cảm ơn rất nhiều vì đã trả lời !. Tôi biết đây hoàn toàn là lỗi của riêng tôi, tôi chỉ thêm nó làm thẻ và quên đề cập đến nó. Nhưng tôi đang mã hóa nó nhanh chóng. Thật không may tôi không có ý tưởng làm thế nào để dịch mã đó để nhanh chóng. – MLyck

+1

Câu trả lời phải có trong nhanh chóng –

+0

@UmairAfzal okay Tôi sẽ được đăng mã cập nhật cho nhanh chóng rất sớm nếu bạn cần nó. –

7

Tôi muốn giới thiệu bắt đầu appdelegate của bạn với bộ xương này:

1) sử dụng hãy để mọi lúc mọi nơi có thể!

2) UINavigationController có thuộc tính rootViewController.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    let viewController = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller 
    let navigationController = UINavigationController(rootViewController: viewController) 

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 
    self.window?.rootViewController = navigationController 
    self.window?.makeKeyAndVisible() 

    return true 
} 
+0

Cảm ơn bạn rất nhiều !!! Câu trả lời chính xác !!!! – Maxim

0
self.window = UIWindow(frame: UIScreen.main.bounds) 
let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let storyboard_Secondary = UIStoryboard(name: "Secondary", bundle: nil) 
var initialViewController = UIViewController() 

let aUser = CommonMethods.loadCustomObject("\(Constants.kUserProfile)") as? User 
if aUser?.respCode == 1 { 
    initialViewController = storyboard_Secondary.instantiateViewController(withIdentifier: "MainTabVC") 
    UIApplication.shared.statusBarStyle = .lightContent 
    let navigationController = UINavigationController(rootViewController: initialViewController) 
    navigationController.isNavigationBarHidden = true 
    self.window!.rootViewController = navigationController 
    self.window!.makeKeyAndVisible() 
} 
Các vấn đề liên quan