更改根视图控制器将IBOutlet设置为nil

时间:2018-11-12 15:14:02

标签: ios swift uiviewcontroller uikit

我必须根据登录状态更改我的根视图控制器。

我更改了AppDelegate文件,并创建了一个扩展UINavigationController的MainNavigationController类。

**问题:-** 加载定义的根类时,它将所有出口变量设置为nil。

代码

 class PostalViewController: UIViewController {

        @IBOutlet weak var btn_currentLocation: UIButton!
        @IBOutlet weak var btn_viewAccount: UIButton!
        @IBOutlet weak var in_postCode: UITextField!

        let locManager = CLLocationManager()

        override func viewDidLoad() {
            super.viewDidLoad()

// Here it gives a nil on btn_currentLocation

            btn_currentLocation.layer.cornerRadius = 15.0
            btn_currentLocation.layer.masksToBounds = true

            btn_currentLocation.setButtonGradient(colorOne: UIColor(named: "lightBlue")!, colorTwo: UIColor(named: "gradient2")!)

            btn_viewAccount.layer.cornerRadius = 15.0
            btn_viewAccount.layer.masksToBounds = true

            // Add search button in post code text field
            let searchButton = UIButton(type: .custom)
            searchButton.setImage(UIImage(named: "iconSearch"), for: .normal)
            searchButton.frame = CGRect(x: CGFloat(in_postCode.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
            searchButton.addTarget(self, action: #selector(self.actionSearch), for: .touchUpInside)
            in_postCode.rightView = searchButton
            in_postCode.rightViewMode = .always

        }

AppDelegate.swift(仅适用于该特定功能)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = MainNavigationController() 
        return true
    }

MainNavigationController.swift

class MainNavigationController : UINavigationController{

    var isLogin : Bool?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        isLogin = UserDefaults.standard.bool(forKey: "isLogin")
        print("IsLogin --->", isLogin!)

        if (isLogin != nil && isLogin!){

            perform(#selector(postalController), with: nil, afterDelay: 0.01)

        }else{

            perform(#selector(showHomeContoller), with: nil, afterDelay: 0.01)
        }

    }

    @objc func showHomeContoller () {

        let homepageController = HomePageViewController()
        present(homepageController, animated: true, completion: nil)
    }

    @objc func postalController () {

        let postalController = PostalViewController()
        viewControllers = [postalController]
    }
}

编辑

我在下面添加了屏幕截图,我必须在HomePage和Postal之间切换根视图

enter image description here

2 个答案:

答案 0 :(得分:1)

我想如果用户已登录,您只想显示PostalViewController。如果未登录用户,则只显示HomePageViewController。首先从应用程序委托中删除这些行,您不需要:

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainNavigationController() 

现在删除整个导航控制器类。您将不需要它,因为您将在HomePageViewController中完成所有这些操作。现在在HomePageViewController swift文件中,将其添加到viewDidLoad()

isLogin = UserDefaults.standard.bool(forKey: "isLogin")
    print("IsLogin --->", isLogin!)

if isLogin != nil {
    performSegue(withIdentifier: "segueToPostal", sender: self)
}

最后,将首页从HomePageViewController设置为PostalViewController

enter image description here enter image description here

并将其标识符设置为 segueToPostal

enter image description here

如果不想让用户从邮政视图控制器中导航回去,则可以将邮政视图控制器嵌入新的NavigationController中。然后只需将HomePageViewController的segue设置为此NavigationController并设置标识符。

答案 1 :(得分:1)

使用nib文件初始化视图控制器时,将设置IBOulet。 因此,您需要使用下面的init方法创建视图控制器

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)

例如,如果您在其中配置IBOultets的xib文件的名称与控制器名称相同,则只需执行下一步:

let postalController = PostalViewController(nibName: "PostalViewController", bundle: nil) 

在这里,您将拥有带有已初始化IBOutlet的控制器。

要使用故事板,您应该从故事板初始化它们。 尝试以其他方式初始化控制器:

let sb = UIStoryboard(name: "MainStoryboard", bundle: nil) 
let vc = sb.instantiateViewController(withIdentifier: "PostalViewController") 

您应该在故事板中为控制器设置故事板ID,如下面的屏幕所示。

storyboard id

此外,您还可以在此处找到更多有关如何从情节提要正确初始化视图控制器的信息

What is a StoryBoard ID and how can i use this?