How can I access IBOutlet in another class?
Your approach is incorrect. A view controller is initiated when it is displayed on the screen. One and only on view controller object can be displayed at one time. In your code, you are initiating a brand new view controller and set text to outlets. So that won't work. Instead, you need to set text to the text field on the existing instance of you view controller.
To do so, in the view controller that you want to receive text field content updates, register in notification center to receive a content update function calls.
NotificationCenter.default.addObserver(self, selector: #selector(listnerFunction(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
func listnerFunction(_ notification: NSNotification) {
if let data = notification.userInfo?["data"] as? String {
self.textField.text = data
}
}
Then in another view controller, if you want to send text to the above view controller and update text, simply post the data to notification center
let data:[String: String] = ["data": "YourData"]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: data)
Accessing IBOutlet from another class
You access a generic ViewController
, but need to use an existing UIView
. Do something like this:
class Test: UIViewController {
class func set_cornerRadius(yourView: UIView, radius: CGFloat) {
yourView.layer.cornerRadius = radius
}
}
That way, you pass the UIView
you want to set the corner-radius.
Swift Accessing IBOutlet from another class
Try this
answerResultView.label.text = "some text"
Access IBOutlet from another class
Step 1: In ViewController2.h create a property on VC2 to reference VC1:
#import "ViewController1.h"
@property (nonatomic, strong) ViewController1 *viewController1;
Step 2: When you create VC2 in VC1, set the property:
[viewController2 setViewController1:self];
Step 3: set the button image on ViewController2
[self.buttonVC2 setImage:[self.viewController1.buttonVC1 imageForState:UIControlStateNormal] forState:UIControlStateNormal];
Note: a better model is to provide a method on VC1 that returns the correct image. For example in VC1:
- (UIImage *)imageForButton {
return [self.buttonVC1 imageForState:UIControlStateNormal];
}
and in VC2:
[self.buttonVC2 setImage[self.viewController1 imageForButton]];
Related Topics
Error: Uitableview Jump to Top with Uitableviewautomaticdimension
Shrink Large Title When Scrolling (Not Uitableviewcontroller) iOS 11
How to Call Https Url in Uiwebview
iOS 5 Gm: <Error>: More Than Maximum 5 Filtered Album Lists Trying to Register. This Will Fail
Ios9: Alternative to Uidevice.Currentdevice().Setvalue(...) to Force Orientation
Uibutton - Alloc Initwithframe: VS. Buttonwithtype:
Handle JSON Response with Alamofire in Swift
Skspritenode Position Changes to 0,0 for No Reason
Swiftui: Animation Inside Navigationview
Swift - Get Date from Day of Year
Customizing Uilocalnotification's Alert
I Added a Localization in Xcode, Removed It and Now My Storyboard Is Gone
Uitableview Strange Layout Behavior Changes on Scroll
How to Show Different Alerts Based on a Condition After Clicking a Button in Swiftui