Xcode add same view from different controllers using class method - xcode

I am using loading indicator with background image and label.
IBOutlet UIActivityIndicatorView *isLoading;
IBOutlet UIImageView *isLoadingImageView;
IBOutlet UILabel *loadingLabel;
In my app this loading set is called like 5 6 times from different controllers. Is there any easy way to write a class like:IndicatorClass and class methods like:showLoadingSet hideLoadingSet to bring this set infront of my view. For example in mapController, i just only want to call [IndicatorClass showLoadingSet] and [IndicatorClass hideLoadingSet].
What I am doing is that I am creating IBOutlets for each controller, it is totally waste of time.
Is there any suggestion?
Thanks

You can have a peak at the MBLoadingHUD example. It sounds like you are trying to implement something similar to this. If not, you can always see how that class is used.

Related

How to programmatically subclass a UIViewController stored in a Storyboard?

I'm working on my first iPad application and using Storyboards for the first time.
I've got UITableViewController in my Storyboard that uses "Dynamic Prototypes" for custom cells.
What I want to do is programatically instantiate my UITableViewController subclasses but loading the super view controller from the Storyboard.
This is because I have a single UI but multiple subclasses for specific functionality that I need for each different instance.
This was very easy to do with .xib files, I would write the following code:
MyViewControllerSubClassA *viewControllerA = [[MyViewControllerSubClassA alloc] initWithNibName:#"generalViewControllerNib" bundle:nil];
MyViewControllerSubClassB *viewControllerB = [[MyViewControllerSubClassB alloc] initWithNibName:#"generalViewControllerNib" bundle:nil];
I know I can assign a subclass in the Storyboard editor when clicking on the View Controller but I want to set the subclass programmatically when instantiating.
This seems impossible to do with Storyboards as they are instantiated automatically.
This makes the entire concept of Storyboards seem flawed and not very OO.
If I move the View Controller out of the Storyboard and into a .xib file I lose the ability to use Dynamic & Static Prototypes cells as these are supported only when using Storyboards. Also Apple's documentation basically says use Storybaords from now on.
I would try something like this:
MyViewControllerSubclassA *controllerA = (MyViewControllerSubclassA *)[self.storyboard instantiateViewControllerWithIdentifier:#"myGenericVC"];

Which object should I use in XCode IB?

I'm messing around with Xcode and Interface Builder. I want to make a timer or clock type app. What type of object should the numbers be? Just a 'Label'?
You could use a 'Label' (which is a modified NSTextField), or you could use an non-editable NSTextField or even a NSView with a custom drawing method.
If you're still learning, I suggest you use a Label and go from there.
When linking to your AppDelegate (or whatever object you're using), remember to create an IBOutlet on your property declaration so you can link your label, like so:
#property (nonatomic, assign) IBOutlet NSTextField *clockLabel;
And setup the clock value using the setStringValue: method:
[self.clockLabel setStringValue:myString];
If you just want to display the numbers, then UILabel will be just fine.
And, if you want some nice looks then i would recommend some nice graphics images for the numbers.

Why is my UIView not the "parent" of my UIImageView?

I have an mainUIView within which there is a customised UIImageView (named myImageView).
In the myImageView.h file, I have declared:
#property (strong, nonatomic) mainViewController *viewController;
and in the myImageView.m file, I have the following codes:
self.viewController = self.parent;
This lines raises an error indication:
Property 'parent' not found on object of type myImageView.
I thought putting the myImageView inside the mainUIView would make the default parent-and-sub relationship, but obviously it's not so.
In the main storyboard, I tried to Ctrl-drag the myImageView to the mainViewController, but no options pops up for me to select delegate.
Can anyone give me explanation of these please? I'm feeling a bit confused... Many thanks!
As your myImageView is a UIView is has no parent. Is just has a superview which again is a UIView. Basically a view doesn't know its controller. Although the controller can implement a view's delegate to get informed about events. So your AppDelegate knows your ViewController and the controller knows your ImageView. The AppDelegates window knows the view of your controller which will most likely contain the whole view hierarchy (except AlertViews, HUDViews, modal views and simmilar...).
I think what you want to do works best implementing the delegate pattern. (see Apple Docs)

how to continuously display time in cocoa?

I have some code to control some buttons for an interface, but I would like to add a small text label that continuously displays time by hh:mm:ss. How do I do that?
well, i think you should take a look at some cocoa development related books or websites - like cocoadev cocoadevcentral cocoabuilder ...
in short:
define your
IBOutlet NSTextField* timeLabel;
in your controller class and hook it up to the NSTextField in your NIB.
take a look at NSDate and update it (via a NSTimer or your draw loop)

IBOutlet not getting set in Cocoa MVC project

I might not be using IBOutlet correctly, or some other subtlety with how NIB files work is causing me trouble - any help would be much appreciated (feel free to propose an alternate way to accomplish what I want).
I have a View object and a Controller object. Both are in the NIB. The Controller's init is also called when the NIB is loaded and the View is initialized in the 'awakeFromNib' callback.
I need a way to connect these two objects - specifically, enable the 'View' object to call functions on the Controller.
Based on documentation online, the way to get these connected is to define an IBOutlet in the View and connect it to the Controller in the Interface Builder. So i created an
IBOutlet Controller* _controller;
in the View interface and graphically connected it to the Controller object in Interface Builder by making a connection from the View to the Controller and assigning the _controller outlet to the Controller (the blue Generic Object box in Interface Builder).
At runtime though, _controller is always _nil. I have verified that the Controller's init was indeed called.
Is there something obvious I'm missing about this?
Any simpler way to connect these two? Since they're both created by the NIB I don't have a common object that has a pointer to both.
Try accessing the IBOutlet in viewDidLoad instad.
When awakeFromNib is called not all the IBOutlets are populated (even though the documentation seem to imply it).

Resources