I have a base view controller BaseUIViewController which is extended from UIViewController. I have two UIButtons at the bottom of this ViewController. Basically I want these two buttons on every UIViewController at the same place through out my app. When I extend BaseUIViewController, I don't see them in the children view controllers.
I have given IBOutlets to the buttons too!
I am new to programming. Please help. Isn't this way inheritance work?
You can create a UIViewControllerContainment. This Stackoverflow's post explains it in detail how you can create it and make it work. Similalry, also have a look at this Stackoverflow's post. Here it is done using childViewControllers.
Make sure you put these button properties in #interface instead of #implement, only properties in #interface can see in subclasses. Here is example:
#interface BaseUIViewController
#property (nonatomic, weak) IBOutlet UIButton *button1;
#property (nonatomic, weak) IBOutlet UIButton *button2;
#end
Both the ways which MUNAHIL answered above, are the best ones I believe. In case you want to achieve this in a more basic way, you can do as below.
Add the two UIButtons in the BaseUIViewController (either programmatically in viewDidLoad or in storyboard). In case you need, you may like to add a UIToolBar first and then the buttons on top of it.
Make other view controllers a subclass of BaseUIViewController.
#interface SomeViewController: BaseUIViewController
Now, all your view controllers will have the two buttons by default.
Related
In order to simplify my question please consider following scenario.
FirstViewController has a UILable which is "connected" as IBOutlet from storyBoard.
SecondViewController which is a subclass of FirstViewController also has a UILable, but has one more UIButton. It has a separate represent in the storyBoard.
I don't know whether it is correct or not.
I ctr+drag the UILable from the viewController of the SecondViewController to the IBOutlet in the FirstViewController.m file.
Working like champ, the association is correct for me. But I still don't know whether it is the correct way to do so. And will it have any bad effect to my viewContorller.
Thanks
I'm learning iOS and am new to the concept of IBOutlets - so I'm making a simple app in an attempt to learn how it works.
The problem:
I created an interface using a Storyboard and want to hook it up to an IBOutlet in my custom class ("TapCounter") in order to access (and be able to set) its text field. However when trying to hook my class' #property (nonatomic, weak, readwrite) IBOutlet UILabel* numberOfTapsTextField;
up to the UILabel in the Storyboard the line does not want to attach to the label.
Here is an image of the situation: http://gyazo.com/0050ef0a78772adcad214cdc4603f932 (Dragging a line from the hollow circle next to the #property to the label in the Storyboard does not snap to it).
I have not modified anything of the boilerplate code except for that I added #import "TapCounter.h" in viewController.m
This feels like it should be a very simple thing - but again; I am new to this.
EDIT
Have I got this idea wrong? Should all IBOutlets be in the viewController of a view (and simply be accessed by other custom classes)?
It work like this:
create a CustomView class
add a view in you storyBoard
set class for this custom view as CustomView in identity inspector cmd+opt+3
Create an IBOutlate of your component inside customView
Link those component with respective outlet
Refer Image:
I want to create UIViewController subclass that will be container for two other controllers and it has properties
#property (nonatomic, strong) UIViewController *firstController;
#property (nonatomic, strong) UIViewController *secondController;
then i wanted to connect these properties via interface builder to instantiate them automatically. But if i just make them IBOutlets
#property (nonatomic, strong) IBOutlet UIViewController *firstController;
#property (nonatomic, strong) IBOutlet UIViewController *secondController;
i cant connect other controllers. but UINavigationController has property rootViewController and we can connect other controllers in IB. if it's possible to make such trick in custom controllers please help
You can't connect outlets from one controller to another controller -- outlets only work within a controller, not across controllers.
If you want a controller to be a container for two other controllers, you should drag two container views into your controller's view in the storyboard. You will automatically get two controllers connected to those container views with embed segues.
It should work the way you have it now. Are you sure you're adding View Controller objects in your XIB? Your UIViewController subclass xib file should look like this:
Then you should be able to right-click and drag the connections for firstController and secondController.
I am trying to implement something similar to AplitView where you can hace separate scenes in storyboard and connect a viewController to another one as an outlet. Is this possible?
#Interface MenuNavigationController : UINavigationController
#property (nonatomic, strong) IBOUtlet MenuViewcontroller *menuViewController;
#end
In interfaceBuilder I want to drag and drop, and assign another viewController to my outlet
Xcode won't allow you to make connections between different scenes in a storyboard. It does allow such connections for the container view controllers that it knows about, like UISplitViewController, but not for your own. See Linking child view controllers to a parent view controller within storyboard for some useful ideas on this topic.
I am creating a simple iOS game that has a counter that tracks the users score and presents it in a UILabel in the corner of the screen. When something happens in the game it switches to a game over screen. I want to then display the contents of the label from the previous view in a label in the new view as the players score.
I feel there should be an easy way of doing this?
Thanks in advance!
The root cause of the problem is that you've embedded your game logic into your user interface. The score is not part of the user interface, but you've put it there anyway.
If your game is really simple, add a property to your app delegate that keeps track of the score. Each part of your user interface can access that property when it needs to.
As soon as you go beyond the most simple game possible, you should factor out your game logic into its own class. That class is responsible for managing the game state. Instantiate it when you start a new game, and you can pass it through to view controllers, create a shared instance, etc.
I'd recommend reading up on MVC.
Just make the label a #property and use self.navigationController.presentingViewController or something similar to access the first view controller:
#interface ViewControllerOne : UIViewController {
IBOutlet UILabel *myLabelOne;
}
#property (nonatomic, retain) IBOutlet UILabel *myLabelOne;
#end
#interface ViewControllerTwo : UIViewController {
IBOutlet UILabel *myLabelTwo;
}
// myLabelTwo doesnt need to be a property, it could just be an ivar. Up to you.
#property (nonatomic, retain) IBOutlet UILabel *myLabelTwo;
#end
#implementation ViewControllerTwo
- (void)viewDidLoad {
[super viewDidLoad];
self.myLabelTwo.text = [(ViewControllerOne *)self.navigationController.presentingViewController myLabelOne].text;
}
#end
UINavigationController Class Reference
UIViewController Class Reference
Properties in Objective-C 2.0
Step away from your views for a second. Your labels and any other views are a place to present data, not to store it. What you want to share across screens is the score, which is part of your player's state.
Create a Player object. Give it a score property. Pass the same Player instance to the controllers of both views. Now you have a model shared by multiple controllers and presented in many views. Either controller can potentially update the model and the other with have access to the new state for display in their views.