Very new to iOS coding. I'd like to be able to display a modal dialog from inside a UIButton subclass. I've used presentModalViewController from inside a UIViewController before. How would I grab a reference to a UIViewController from my subclassed UIButton?
Can I somehow discover it's parent? Implement a protocol in the parent UIViewController that I can set as a delegate on my UIButton subclass?
You should be able to use any subclassed UIButton just as a normal UIButton. Setup an action that is triggered on the button which will call a method on the view controller. The view controller will deal with pushing the modal view controller.
Although there are ways in which you can find out the view controller from the button when it is attached to a view, this is a better approach.
Related
My app header is currently a full width, 65pt height UIView which I then use as a generic header for all pages.
class AppHeader: UIView {
...
}
Then, in my Main.storyboard I have a UIViewController with a View from the object library which has its class specified as AppHeader.
My AppHeader (UIView) has multiple buttons which should, if clicked, take you to from the page/controller you're currently on to another.
From the AppHeader class I do not have access to use the present method to show another controller as its not within scope.
Here is my AppHeader.xib:
How can I resolve this?
This is very bad behaviour, you should not use a UIView as a header. You should add a Navigation View Controller as the first view controller of your app. Navigation view controller has the navigation bar where you can put the buttons you want there. From that ones, you will be able to push or present other view controllers.
Check the official documentation: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html
Make your AppHeader view a custom subclass of UIView if it isn't already. Wire the actions on the button to IBAction methods in the view.
Create a protocol AppHeaderDelegateProtocol. Give your AppHeader class a weak delegate property. Define methods in that protocol that let the AppHeader notify it's owning view controller about button presses.
Implement your AppHeaderDelegateProtocol in view controllers that will contain instances of AppHeader.
Connect the delegate property to each instance of AppHeader's owning view controller.
That should do it.
You can create a protocol - call it AppHeaderDelegate or something - and set up your other viewControllers to adopt that protocol. You could define functions to let your delegate know that a certain button was pressed, and your delegate viewController can react to that by presenting the correct view controller.
Apple's docs on protocols here.
Alternatively, you can use NotificationCenter to broadcast notifications to let subscribers know that a certain button was pressed, and have your viewControllers listening for these notifications and reacting to them accordingly. You have to manage when classes start/stop listening, though, as you may have several objects trying to react to a single notification.
Without storyboards, I can simply assign the UITabBarController.delegate = self (in AppDelegate, for example). This lets me handle what happens when a UITabBarButton on the UITabBar is pressed.
However, I'm now using storyboards, where my UIViewController is contained in a UINavigationController, which is contained in a UITabBarController.
What's the best way to access this parent UITabBarController in code? I'd like to do this so I can assign a delegate to it. When the user presses a button in the UITabBar, I want to take an action in my contained UIViewController.
self.navigationController?.tabBarController
If I'm using a UINavigationBar without a UINavigationController and I'm laying it out in a storyboard, and I want to set the delegate, do I have to make an outlet and attach the delegate on viewDidLoad or is there some way that I'm missing to attach my view controller as a delegate for UINavigationBar declaratively?
Should be able to drag from the Outlets view in IB to the ViewController and set that as the delegate. See attached.
In my project i have an UIView instance and attached a gesturerecognizer to it. In case that gesture is recognized i want to call its parent viewcontroller to reorganize the scene.
Here's how it's implemented
The UIViewController is called HomeViewController.
It has one subview which is a UIScrollview.
The UIScrollView contains several UIView instances.
To all of these UIView instances i attached a gesture recognizer. When it fires i want to disappear and call the HomeViewController's reOrganizeUI method.
My problem is that i can't reach the HomeViewController from the UIViews.
Is there a way to do this?
Sincerely, Zoli
Surely you can. The simplest way to do this is to add a property to all your UIView subclasses and assign your HomeViewController to that property when creating the views in the view controller's initialization method. Then you'll be able to access the controller from the views directly.
Simple question:
My MainWindows organize all Views I use.
When I click a button on view1 i want that view2 is shown.
How to manage this?
You typically manage the views within a UIViewController subclass. If you created the views with Interface Builder you must connect them to the view controller by an IBOutlet so you can access them in code. Then in the button handler method you can simply set the view's hidden property to YES to hide a view or NO to show it.