Working with xib and viewController - ios

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
}

Related

Why viewDidLoad function is not called?

I want to change UIBarButton's color in the navigationBar. To achieve this, in viewDidLoad: I put this line:
navigationController?.navigationBar.tintColor = .white
Everything works fine until I started to notice something strange. That UIBarButton is used to dismiss the UIViewController. When it is pressed, I just dismiss the viewController. But, if I present it (viewController) again, the color of the UIBarButton is not white, it gets tintColor of the application.
After doing some debugging, I noticed that viewDidLoad: is not called again after the viewController is just dismissed and presented again. The reason why my UIBarButton has a different color is because I change its color in viewDidLoad:. When viewDidLoad: is not called, of course, color is not changed.
It was an interesting discovery for me the fact that iOS doesn't call viewDidLoad: for UIViewController that was presented already. Possibly, it is due to the optimisation, because it is not efficient to draw the whole UI every time.
My solution to this problem can be to change color, not in viewDidLoad:, but in viewDidAppear:. But, is it right approach to solve a problem? And why viewDidLoad: is not called in the above situation?
It looks like you create and store you view controller, but present it wrapped in UINavigationController:
let controller = YourModalViewController()
...
func presentMyModal() {
present(UINavigationController(rootViewController: controller))
}
In this case your viewDidLoad method will be called just once and you'll have visual bug. If you want to leave styling code of your modal inside it's file you can create instance func which will return this controller wrapped and styled.
extension YourModalViewController {
func wrappedInNC() -> UINavigationController {
let nc = UINavigationController(rootViewController: controller)
// Styling code.
return nc
}
}

Ios - action on button in nib file

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.

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.

Adding UISegmentedControl to UINavigationController's Toolbar with Storyboard

I'm trying to add a UISegmentedControl to a UIToolbar using IB within xCode.
Whilst this is easy using a xib file, (create a view, add a toolbar to the bottom of the screen and drag on a UISegmentedControl onto the toolbar), it seems 'impossible' using storyboards.
I have a UIViewController scene embedded in a UINavigationController...
I have the following settings on the NavigationController...
I seem to be able to add UIBarButtonItems to the toolbar in the ViewController, not a UISegmentedControl as I can with xibs.
It even seems impossible to do via code...
let segmentControl = UISegmentedControl(items: ["One","Two"]);
let segmentedControlButtonItem = UIBarButtonItem(customView: segmentControl);
let barArray = [segmentedControlButtonItem];
self.navigationController?.setToolbarItems(barArray, animated: false);
Surely this is doable via storyboards? I can add UISegmentControls to the top bar in the Navigation Controller, (via a UIView) but not to the bottom toolbar.
How can this be done?
You need to set the toolbarItems of your own ViewController, this will propagate to the NavigationController:
self.toolbarItems = barArray
I haven't found a way of adding a SegmentedControl via Storyboards, which I would prefer as well.

Embedding subview in main view Swift

If you look at this image (I don't want to upload it, as it does not belong to me), you will see what appears to be a uiview inside the main uiview controller in the settings app of an iPad. My question is, how do I replicate this programatically? In other words, how do I embed a UIView in another?
Here's what I know and have done:
Based on my research, this is called "subviews". Is this correct?
I have created a UIView with the proper elements that I want in interface builder. It is currently not a subview, but at the same level hierarchically as my main view.
I found the same exact question except in Obj-C, not my language (Swift).
Here's what I need:
How do I programatically spawn a subview in swift once a button is clicked?
As this subview is pretty complex in terms of UIelements, I want to be able to design it in interface builder.
Here's what I have so far:
#IBAction func buttonPressedSpawnSubview(sender: AnyObject) {
//Open a subview from Interface builder.
}
#IBAction func closeButtonPressedSpawnSubview(sender: AnyObject) {
//kill the subview.
Can someone help me figure out how fill in the commented lines?
What you are seeing is a modal UIView on top of another view.
An example from within your visible ViewController would be:
self.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal // Choose whatever transition you want
self.modalPresentationStyle = .CurrentContext // Display on top of current UIView
self.presentViewController(yourNewViewObject, animated: true, completion: nil)

Resources