Display object on all view controllers - ios

I have several objects which I display throughout all of my ViewControllers.
I am wondering if there is a way to merge these objects into a single object and initialize the object from every ViewController in the project.
In essence, I am attempting to merge the objects (they are the same type and do the same thing) and make them globally accessible.
Ex.
#property (weak, nonatomic) IBOutlet UIProgressView *progressView;
I would like to display and access this UIProgressView from all ViewControllers.

You can put these interface elements in a UIViewController subclass and then have all of your other viewControllers inherit from that one (i.e. an abstract superclass) in this way there is only one lot of code which covers them all.

Related

Extend UIViewController's UI down to children UIViewControllers

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.

Passing data from parent ViewController to child UITableViewController via Object

I have a couple of delegates that work in my code already (not asking how to use them). I'm having a problem getting data from the parent ViewController (More Information in the storyboard belongs to class InputViewController) to the child TableViewController (AddressTableViewController) as I'm unable to reference the parent controller from this class.
Both TableViewControllers and the map are in InputViewController. I want to send data (the reverse-geolocated address from locationManager) from the map to AddressTableViewController to fill in the cell text. The TableViewControllers are tied to the InputViewController (main controller) via an Object (selected in the object library).
Code: https://gist.github.com/damionjn/c148a07a39b5c27e7f78
You'll see I'm trying to use the protocol at the bottom of InputViewController here:
https://gist.github.com/damionjn/c148a07a39b5c27e7f78#file-gistfile1-swift-L125
But I'm unable to use it as I have no access to InputViewController, thus mapAddressDataSource is always nil. I've tried creating an instance of it in AddressTableViewController's init() and creating an IBOutlet without any luck.
I'm having a problem getting data from the parent ViewController (More Information in the storyboard belongs to class InputViewController) to the child TableViewController (AddressTableViewController) as I'm unable to reference the parent controller from this class.
Why not?
Create a property on AddressTableViewController for your InputViewController but create it as weak and you can access it without causing any problems (retain cycles).
e.g.
#property (weak, nonatomic) InputViewController *parentInputViewcontroller;
Then you should be able to access any of the data on self.parentInputViewController from within your tableView subclass.

How to reuse UITableView from another class

in my apps i want to to show a UITableView that exactly same with another UITableView from another class.
Let say in my HomeClass.h i already have UITableView property:
#property (nonatomic, strong) IBOutlet UITableView *fromTable;
and already success implement this fromTable.
However, i also want to use this fromTable again from other class, lets say DetailClass.
So when for example, i push a button in DetailClass, the button will show fromTable, also with all the delegates function like didSelect, etc.
Can i do that instead create other UITableView again in my DetailClass?
Thanks...
Refactor the UITableView's Delegate and DataSource methods into an object that you use in both Classes (presumably UIViewController subclasses).

Should UIViews have properties?

Using proper MCV with Objective-C can a UIView subclass have #propertys?
i.e. in the .h file
#class MyViewSubclass;
#interface MyViewSubclass : UIView
#property (strong, nonatomic) UILabel *labelLabel;
#property (strong, nonatomic) UILabel *valueLabel;
#end
or should this be done in a UIViewController subclass?
Thanks in advance.
It is most common to subclass UIViewController to manage the labels, fields, images, and other views within a view hierarchy. However, if you are creating a reusable component view that will be used throughout your application, then it's perfectly appropriate to subclass UIView and add properties to your subclass.
From Apple's iOS App Programming Guide:
View controller objects manage the presentation of your app’s content on screen. A view controller manages a single view and its collection of subviews. When presented, the view controller makes its views visible by installing them in the app’s window.
The UIViewController class is the base class for all view controller objects. It provides default functionality for loading views, presenting them, rotating them in response to device rotations, and several other standard system behaviors. UIKit and other frameworks define additional view controller classes to implement standard system interfaces such as the image picker, tab bar interface, and navigation interface.
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AppArchitecture/AppArchitecture.html#//apple_ref/doc/uid/TP40007072-CH3-SW1
It's good for views to have properties, but don't mix model logic into a view. The properties in a view should describe how the property looks, not what the property holds. I would avoid having a property named valueLabel in a view.
An example of view property names is UITableViewCell. It has properties imageView, textLabel, and detailTextLabel.
It's perfectly reasonable for a UIView subclass to have properties. You might need them to implement layoutSubviews, for example.
It is perfectly reasonable, also if you want to create a reusable component that allows for interaction and better flexibility, take a look at UIControl (which is a subclass of UIView)

Carrying the contents of a label from one view controller to another

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.

Resources