Adding an IBOutlet to custom class - ios

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:

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.

The storyboard is automatically copied to the new project

I have a project in Xcode, lets name it A, in which I have a story board and several views. I only need part of project A. Hence, I made a new project, B, and made a new UIViewController, with xib, and copied the two UIViews from project A to B.
It looks like this now, where RPM Display and Speed Display are copied (by dragging from xib) from A to B.
AnalogDisplayContainer belongs to the class PD_AnalogDisplayContainer, and we have two outlets in class PD_AnalogDisplayContainer:
#property (nonatomic,assign) IBOutlet PD_AnalogDisplay* speedDisplay;
#property (nonatomic,assign) IBOutlet PD_AnalogDisplay* rpmDisplay;
which are connected to the respective views in xib. (We have exactly the same code in project A.) These UIViews have some UILabels as well, which are automatically copied in project B, and I need them as well. You see them here:
Up to here, the description of what I did. Now, the problem:
I have three outlets in class PD_AnalogDisplay.
#property (nonatomic, assign) IBOutlet UILabel* unitLabel;
#property (nonatomic, assign) IBOutlet UILabel* digitalIntegerValue;
#property (nonatomic, assign) IBOutlet UILabel* digitalFloatValue;
However, so strangely, when I click on the small point beside these outlets, it shows me the connections to story board,
and when I click on the connection, I see the whole storyboard and views from project A.
In addition, these connections are not shown when I click on xib.
I searched in project B, there is no storyboard, or all these things I see in the previous picture, in form of code. So I don't know where it all come from. And I get the run time error:
loaded the "RPMViewController" nib but the view outlet was not set.
Yes, it brings previous reference wherever you copy it.
It is happening because the class name is same in project A and B.
Create class name in project B other than class name in project A or slightly change variable name.
Assign that class to your xib file.
You must recreate the IBOutletes from xib file.
Just copy the interface components not the code in .h file.
Observations:
When I move project A to another location makes trick, reference destroyes.

Cannot create outlet connections to subviews in Interface Builder (Xcode 5)

I know this appears to be a duplicate of some other questions, but the answers are not working for me.
I have created a single view app.
In the storyboard I added a subview to my main view.
I have a label on my main view and another label on my subview.
I have created a class of type UIView and added it as the custom class for the subview.
I can ctrl-drag my label on my main view to the main view controller class. But when I try to ctrl-drag my label on my subview to my custom class, I cannot get the connection to occur.
I have even typed the property information and tried to make the connection manually to no avail.
Things have changed a bit in the latest version of Xcode's Interface Builder. Can somebody tell me what I am missing? There is literally no code here. I am just testing trying to connect outlets to a subview with a custom class.
The first image shows that I have set up the custom class and added a property but I cannot make the connection.
The second image shows the main view label is connected in the main view's controller.
The third image shows that there are no outlet connections for the subview's label.
You can manually write the IBOutlet property declaration in the #interface of the custom view subclass, and assuming you've defined the base class of your subview in IB, then you can drag from the outlet circle in the code back to the control in the scene.
Or, as you point out, Warren Burton suggested both this technique and another in his answer to this other question, Can't Wire to Subview in IB.
The issue has to do with the File Owner of the View Controller. It is probably set up as being IOViewController, thus you can only make property connections in that .h file.
What you can do, is create another .nib file for the subview and put the subview in there. Then in that .nib file, make the file owner IOSubview. Property connections will work just fine there. Then just add the subview to your IOViewController programatically. Just remember to load the nib file from bundle first.
This is what I did (in Swift):
I Created a new ViewController (e.g. class MyViewController: UIViewController {})
In StoryBoard, I expanded the 'Scenes' (i.e. the tree view of all UI components) and selected 'MyViewController'
Using the 'identity inspector' I assigned the 'MyViewController' class (as oppose to the default UIViewController)
After that I was able to assign an action.
I suspect that for Obj-C it is similar process.
You don't create outlets in the subclass, you create the outlet on the view controller it is on. You need to #import the subclass into IDViewController.h and create an outlet there.
IDViewController.h
#import "IDSubclass.h"
...
#property (strong, nonatomic) IBOutlet IDSubclass *outletName;
Zoom your storyboard to 100%. If you zoom out, to say 50%, then the outlet connection won't work.

I can't create an outlet in Xcode 5

I am trying to link a create an IBOutlet for a UIWebView Element, however whenever I hold down control and drag from the Storyboard to the ViewController.h file, nothing happens and an outlet is not created. I already have an outlet in this file, however, I would like to create another one. Whenever I go to the view controller for the previous outlet and hold control and drag the element to the ViewController.h file a new outlet is created. Is there a setting that I have enabled that stops this shortcut from working. Is there another way to create an outlet for ios in Xcode 5.0.1 on Mac OSX 10?
Fixed issue. The class of the view controller was UIViewController not ViewController. This meant that the ViewController.h and .m files were not showing up under Automatic. Once changed I could view the ViewController.h and .m files and create outlets in them.
I recommend not using storyboard, while it is a lot easier to use, it doesn't let you do many things. To create a new outlet, go to where you want to create it (this will most likely be a header file) and then type: IBOutlet yourOutletType *yourOutletName so to create a UIWebView you would type:
IBOutlet UIWebView *myWebView
and to create an outlet, such as a UIImageView, you could type
IBOutlet UIImageView *imageView
just remember to hook it up in your .storyboard file.
Again, I recommend not using storyboard, it IS a lot easier, yet it really has lots of limitations

iOS MediaPlayer makes application crash

If I start with the very minimalist UI implementation consisting of adding a single UIButton (with the corresponding IBOutlet) to the default view in the default storyboard and adding an action method to my controller class, then everything works correctly (i.e. my action gets invoked when I tap the button).
Now, if I simply include MediaPlayer/MediaPlayer.h to my view controller header file, like that:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface Playing_Video_FilesViewController : UIViewController
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
#property (nonatomic, strong) IBOutlet UIButton *playButton;
#end
and add the MediaPlayer framework to the project, it crashes with the infamous "this class is not key value coding-compliant for the key playButton".
If I remove the MediaPlayer framework reference from the project, it stops to crash. If I instead remove the button but leave the MediaPlayer framework, it stops to crash as well.
Is there something else I should do to use MediaPlayer and a basic standard UI at the same time?
Edit:
I circumvented the problem by creating a .xib file instead of using the storyboard, like how it's done in the VideoPlayer sample application which doesn't use storyboards. Same code, but now it works.
I see this whenever I have an outlet or action that is no longer hooked up properly.
1.) Open storyboard
2.) Select playButton
3.) Remove all outlets and actions from this item
4.) Remove plugs from your class that were previously attached (especially in dealloc)
5.) Create new outlet and control-drag from the storyboard to your class. Good to go.
-A little overkill but gets the job done for me everytime.

Resources