Ios - action on button in nib file - ios

Good evening,
I'm wondering if displaying a nib file as a subview is the more standardized way of displaying a subview when compared to hiding and unhiding a view of the same class.
Also,
How would i be able to set an action on the buttons in the nib file?
let test = xWork.loadViewFromNib()
test.center = view.center
self.view.addSubview(test)
Currently doing the above. Even though my xWork nib has a class with an outlet for a button, i'm unsure as to how to set an action to it.
Thank you

Please clarify your first question. As for your second question, how to set the action of a button in a nib, try this:
In your nib, create a callback closure:
var onClickCallback: (Void -> Void)?
and in your button's IBAction, call the closure
#IBAction func buttonAction(sender: IBAction) {
onClickBallback?()
}
Then, where you use your nib, set the button callback:
let test = xWork.loadViewFromNib()
test.center = view.center
self.view.addSubview(test)
test.onClickCallback = {
print("Button clicked!")
// Try this to dismiss the view.
test.removeFromSuperview()
}

If your nib file owner is its super view(or view controller),just CTRL+DRAG an target-action.

Related

Link an #IBAction to a button in a .xib view from a ViewController

I have created a .xib file for a view that is going to be repetitive in my iOS app, inside of which there is a UIButton.
I have included that .xib view inside multiple UIViewControllers in my storyboard. I would like to link an #IBAction and an #IBOutlet to the button inside my .xib view that is specific to each UIViewController. In other words, I want every UIViewController to completely manage and handle the UIButton that is inside the .xib view.
Any idea if the above is feasible?
There are a couple of ways to do what you want.
The way I would do it is to give your custom view a closure that is run when the IBAction method is triggered. And each view controller that loads the view from the xib can pass in the closure to the view and the action will run when the button is clicked.
So here's the best solution I came up with so far.
Inside my .xib, I link the button to an #IBAction that is empty.
Again inside my .xib, I created a protocol with a single method that I will call inside the #IBAction created in step (1.)
The #IBAction will run the protocol method every time it is called, so every time the button is clicked.
Implement the protocol stub in every ViewController that needs to handle the #IBAction, and make sure to link that ViewController to the .xib using the protocol created in step (2.)
In your ViewController witch contains the xib view, just asign an action to the button inside xib view
class YourViewController:UIViewController{
override func viewDidLoad(){
//ListTitleView : a xib view, action button witch called theManageButton is inside it.
let theListTitleView = ListTitleView.init(frame:CGRect.init(x: 0, y: 0, width: self.view.frame.width, height: 100))
//Add action to theManageButton
theListTitleView.theManageButton.addTarget(self, action: #selector(yourFunction(Sender:)), for: .touchUpInside)
}
func yourFunction(Sender:UIButton){
//...Do something here
}
}

Working with xib and viewController

I have one button in .xib file.
I want that if I will click on that button .xib file color of viewController should be changed.
How can I do that?
Should I use NSNotification or is there any other way to do this?
A UIViewController doesn't have a color. You might be talking about its root view, maybe tintColor from a UINavigationBar if it's embedded in a UINavigationController?
In any case, a IBAction connected to your UIButton will do what you want.
Connect your UIButton on xib with an IBAction within your class inherited from UIViewController and change the view property of UIViewController. You can do it using self.view.backgroundColor
Here is simple code snippet to do that. Hope it helps.
#IBAction func myButtonPressed(sender: AnyObject) {
self.view.backgroundColor = UIColor.redColor() //Or any color of your choice
}

UITextField inputView - IBActions not fired

I want to replace the default keyboard of a UITextField with a custom keyboard. So I created a new subclass of a UIViewController with a xib-file (the other way like creating both files seperately and setting the File's Owner doesn't work either).
Then I added a button to the KeyboardView and connected it to an IBAction. After that I set the textfields inputView to the new view like this:
override func viewDidLoad() {
let keyboardVC = KeyboardViewController()
textField.inputView = keyboardVC.view
keyboardVC.view.autoresizingMask = .FlexibleHeight
keyboardVC.delegate = textField
}
It's working and the custom keyboard shows up, but if I touch the button, the IBAction is not called. What's the problem in my setup? (I checked some examples and they all do it the same way).
UPDATE:
I now removed the ViewController and subclassed the UIView. Now the actions are working. Why isn't it working with a ViewController?
Since no one holds the UIViewController- there is no reference to it after the viewDidLoad() ended, it is released from the memory.
When the button is pressed, the view controller that should response to the action is not exist -> you are holding only the view of the view controller as the textField.inputView.

iOS - is it possible to inherit view controller with xib file? and how?

I have a few view controllers which would have same background image and one or two buttons. Rest of the content would be different for each controller. I would like to create BaseViewController which would have .xib file and in which I would set background image, buttons and other stuff with constraints. Then I would like create subcontrollers (HomeViewController, GameViewController and so) which just inherit from BaseViewController and have all stuff set in Interface Builder. Is it possible? And subcontrollers would have set own stuff in Storyboard? Background image set in .xib for superclass and tableView in Storyboard for subclass. I know it would be possible when I would set all stuff in code but is it possible with .xib and IB?
I was thinking about usage of Container view but it's possible that I would like to change BaseViewController and maybe create more supercontrollers so I think if it is possible it would be easier with inheritance.
Edit:
Possible way suggested by iphonic. It's based on two controllers that together enabled other controllers to be subclass. BaseGameDesignViewController has .xib file and no more code in controller other than default. Code below is from BaseGameViewController from which inherits other controllers. This has problem with unwind segue which when rolling down has white screen.
override func viewDidLoad() {
super.viewDidLoad()
var viewController = BaseGameDesignViewController(nibName: "BaseGameDesignViewController", bundle: nil) as BaseGameDesignViewController
contentView = viewController.view
viewController.homeButton.addTarget(self, action: "homeButtonTapped:", forControlEvents: .TouchUpInside)
self.view.insertSubview(contentView, atIndex: 0)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
contentView.frame = self.view.frame
}
func homeButtonTapped(sender: AnyObject) {
self.performSegueWithIdentifier("backToMainSegue", sender: self)
}
This is impossible.
The best solution you can achieve is either based on:
Container views
having a dedicated view XIB which is then included into every controller
Outlets defined in superclass but copy-pasting everything in Interface Builder.
you can avoid code duplication but you won't avoid duplication in IB.
No, it is not possible to inherit view controllers with .xib files.
However, you can use Container View for reusing subviews for your so called child views. You have to create common subviews used in different view controllers in a container view.Click here

Programmatically created custom view with button target

I'm having an custom UIView subclass which has some UIButtons appended. For every button there is an target action defined on the UIViewController which is including the custom UIView.
Somehow the TouchUpInside event is never being caught within the custom view including ViewController. Since UIViews are part of the UIResponderChain I wonder why events are not being fired? Does it make any sense to delegate the target action or should this be done another way?
Custom UIView Subclass: CustomView
let button = UIButton(frame: CGRectMake(10.0, 10.0, 100.0, 100.0))
button.addTarget(self.delegate, action: "buttonTapped:", forControlEvents: .TouchUpInside)
ViewController
// Include the custom view
let customView = CustomView()
customView.delegate = self
self.view.addSubview(customView)
...
func buttonTapped(sender: AnyObject!) {
// Doesn't get called
}
Do you set userInteractionEnabled to YES on your custom view? It's NO by default on views that are not descended from UIResponder (mostly buttons).
If a view's userInteractionEnabled = NO, it will ignore touch events.
I assume self.delegate is nil when you adding target...
Few solutions:
initialise custom view with delegate (create custom function initWithFrame:delegate)
override setDelegate function for custom view, and add target to button there;
make button variable public, and manually add target to it in your external class
Unfortunately I was missing to declare the frame rect for the buttons - so buttons are being displayed, but they're indeed not clickable.

Resources