本文介绍了ViewController对象为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个视图控制器.
1. ViewController(VC)
2. WebViewController(WVC)

I have 2 view controller.
1. ViewController (VC)
2. WebViewController (WVC)

在VC中,我单击一个按钮,然后显示WVC.在WVC中成功完成所有任务之后,我想展示VC并在VC中执行特定功能.

In VC I click on a button and WVC is shown.After successfully completing all tasks in WVC, I want to show VC and execute a particular function in VC.

现在,情节提要包含一些对象,并通过单击和拖动将其添加到VC.最初,它具有一些值(不是nil).我单击按钮,按下WVC,完成WVC中的所有任务后,推送了VC.

Now, Storyboard contains some objects and add to VC using click and drag.Initially, it has some values(not nil).I Click on button, WVC is pushed, After completing all task in WVC,VC is pushed.

现在VC中的某些值为nil.错误,欢迎( UILabel UIView )为零...而且也是想在VC中调用函数.怎么可能.?

Now some values in VC is nil.error,welcome (UILabel, UIView) is nil...And also is want to call a function in VC.How it is possible.?

class LoginController:UIViewController,UITextFieldDelegate{
    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var error: UILabel!
    @IBOutlet weak var welocome: UILabel!

    ......

    @IBAction func BtnClicked(sender: AnyObject) {
     let webViewCnt = self.storyboard!.instantiateViewControllerWithIdentifier("WebViewController") as UIViewController
        self.navigationController?.pushViewController(webViewCnt, animated: true)
    }
    func doSomeProcess(){

    }
}

WebViewController.swift

class WebViewController: UIViewController, UITextFieldDelegate,UIWebViewDelegate {
 override func viewDidLoad() {
    super.viewDidLoad()
    webView.delegate = self

    self.start()
}
 func start(){
      ---- do some process ----
      dispatch_async(dispatch_get_main_queue(), { () -> Void in
        let ViewCont = self.storyboard!.instantiateViewControllerWithIdentifier("LoginController") as UIViewController

        self.navigationController?.pushViewController(ViewCont, animated: true)
        ViewController().doSomeProcess() // call viewcontroller doSomeProcess() function
       }
   })
 }
}

推荐答案

您将VC推向WVC的方法很简单,因此,在将WVC推送到NavigationController之后,从逻辑上讲.它将WVC添加到了Navigation Stack的顶部.&最好从导航"中弹出此WVC,它将自动显示具有先前值的VC.

Simple your are pushing VC to WVC,So logically after pushing WVC to NavigationController.it added WVC to the top of Navigation Stack.& it it better to pop this WVC from Navigation will automatically show VC with previous values.

使用此

class LoginController:UIViewController,UITextFieldDelegate{
    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var error: UILabel!
    @IBOutlet weak var welocome: UILabel!
    var isBackFromWVC = false
    ......

    @IBAction func BtnClicked(sender: AnyObject) {
       let webViewCnt = self.storyboard!.instantiateViewControllerWithIdentifier("WebViewController") as UIViewController
        self.navigationController?.pushViewController(webViewCnt, animated: true)   
       isBackFromWVC = true
    }
    func doSomeProcess(){

    }
    func vieWillAppear(){
      if(isBackFromWVC){
        doSomeProcess()
      }
   }
}

WebViewController.swift

class WebViewController: UIViewController, UITextFieldDelegate,UIWebViewDelegate {
 override func viewDidLoad() {
    super.viewDidLoad()
    webView.delegate = self

    self.start()
 }
 func start(){
      ---- do some process ----
      dispatch_async(dispatch_get_main_queue(), { () -> Void in

         self.navigationController?.popViewController(animated:true)
       }
      })
  }
}

这篇关于ViewController对象为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:29