call IBOutlet UIButton from one class to another V

2019-07-23 19:27发布

I have one view controller called dataviewcontroller.Another view controller called scopeviewcontroller

In dataviewcontroller i have an IBOutlet of UIButton called AddData.I want to call this button in my scopeviewcontroller. And I want to hide that button.

I have used this view controller in my pager. I want to hide AddData button in my scopeviewcontroller which is present at the third page.

I tried this code in viewdidload in my scopeviewcontroller.

But is m getting this error: Value of type 'UIViewController' has no member 'AddData'

my code in first view controller:

class dataviewcontroller: UIViewController {

    @IBOutlet var AddData: UIButton! 
}

my second view controller:

class scopeviewcontroller: UIViewController,UITableViewDelegate, UITableViewDataSource {

 override func viewDidLoad() {
        super.viewDidLoad()
let storyboard = UIStoryboard(name: "data", bundle: nil)
       let controller = storyboard.instantiateViewController(withIdentifier: "dataviewcontroller")

        controller.AddData.hidden = true

    }


}

Please help me out!...Thanks

4条回答
贪生不怕死
2楼-- · 2019-07-23 19:36

An outlet doesn't instantiate because an outlet is a variable (or property).

The objects in a nib are instantiated when that nib is loaded, and they are assigned to each outlet as immediately as possible afterward, after the objects are created but before awakeFromNib is sent to all relevant objects.

Check ans by Peter Hosey here for detailed explanation.

In your case you can pass a bool var from scopeviewcontroller to dataviewcontroller. Based on conditions you can set your AddData button hidden property true or false in dataviewcontroller's viewDidLoad or viewDidAppear.

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.AddData.hidden = true
}
查看更多
聊天终结者
3楼-- · 2019-07-23 19:37

You have to Type Cast the dataviewcontroller from UIViewController because by default it will return to the parent class UIViewController object that doesn't have the AddData button, You have to do just like that:-

var needHideAddData = true

class scopeviewcontroller: UIViewController,UITableViewDelegate, UITableViewDataSource {

 override func viewDidLoad() {
        super.viewDidLoad()
        let storyboard = UIStoryboard(name: "data", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "dataviewcontroller") as! dataviewcontroller
        controller.AddData.hidden = true
    }
}

NOTE

Basically, it will not be accessed before initialization the UIViewController nib so after loaded the nib the references and the memory will be allocated to the IBOutlet so you have only two way to implement that:

1:- By default you have to make it hidden and when you need unhide you can control in the dataviewcontroller viewDidLoad method.

2 And the second one write code in viewDidAppear or viewDidLoad in dataviewcontrolleris like luckyShubhra tells:

class dataviewcontroller: UIViewController {
    @IBOutlet var AddData: UIButton! 
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.AddData.hidden = needHideAddData
    }
}
查看更多
够拽才男人
4楼-- · 2019-07-23 19:46

create object of dataviewcontroller in scopeviewcontroller. eg. var dataVCObj = DataViewController(); than using object dataVCObj you can access the iboutlet of button. eg. dataVCObj.AddData

查看更多
Fickle 薄情
5楼-- · 2019-07-23 19:53

You no need to access the button on every view controllers, which you added on the pager. You can hide the button on the scopeviewcontroller itself. with the help of CAPSPageMenuDelegate

In ScopeViewController, set the delegate of your pager.

fun viewDidLoad() {
   super.viewDidLoad()
   pageMenu.delegate = self 
}

And implement the protocol CAPSPageMenuDelegate

extension scopeviewcontroller: CAPSPageMenuDelegate {
    func willMoveToPage(_ controller: UIViewController, index: Int) {
        self.AddData.isHidden = (index == 2)
    }
}
查看更多
登录 后发表回答