Custom UIView from outlet - iphone

I know this has been discussed a number of times but I still have some problems getting around the problem, so any help would be appreciated. I have a class MyView which subclasses UIView. So far so good, in this custom class I basically configure the layout, UI, etc.
Now, I have a separate UIViewController and what I want to do is create an outlet for that custom view. In the view controller's Nib I set the view's class to that of my custom view and connect it to the outlet, but I can't see anything apart from a blank view.
How and where do I load the view from nib? Do I simply say self.theOutletForMyCustomView = load from nib or is it something else? Thanks for your help

First of all, you have to set the name of your CustomView inside your UIViewController nib file like that
Then, you have to retain your property like that inside your UIViewController interface :
#property (nonatomic, retain) IBOutlet CustomView *myCustomView;
or an ivar should work, but assign an IBOutlet property doesn't work.
And if you customize your CustomView inside your CustomView class implementation. Beware of doing your initialization in awakeFromNib or initWithCoder: instead of initWithFrame:

Related

Unable to assign class to image on storyboard

I have dragged an Image View to my story board, and have created a class called Logo
#import <UIKit/UIKit.h>
#interface Logo : UIView
#property(strong,nonatomic)UIImageView *logo;
-(void)loadLogo;
#end
However, when I try to assign Logo class to my Image View on storyboard in Identity inspector, it does not recognize Logo as a legit class and thus I am unable to assign it. Why is this happening? Logo is a subclass of UIView isn't it? How do I fix this problem?
You have already answered your question. Your problem is because you want to assign your Logo class which is inherited from UIView, with a UIImageView. This won't work.
You have to assign your View with your class under Identity inspector.
Then modify #property(weak,nonatomic) UIImageView IBOutlet *logo; property, and you can assign it.
Add your View into your VC, then assign your View with #interface Logo : UIView, Then modify #property(weak,nonatomic) UIImageView IBOutlet *logo; , Add an UIImageView to your View in Interface Builder, then you can assign your IBOutlet *logo; with the UIImageView what you placed there
If you place a UIView on your ViewController, of corse you can assign it with your custom class only if it is inherited from UIView.
If you place a UIImageView on your ViewController, of corse you can assign it with your custom class only if it is inherited from UIImageView.
and so on...
You have to create an IBOutlet
UIImageView IBOutlet *logo;
1 - Drag a view object from the objects library and add it to a view in your storyboard.
2 - Select it and set its class to be Logo
3 - Add an image view to it
And now you'll be able to assign the image view to be the logo outlet.
If the only way you'll be adding logo is through storyboard then your interface should look like this :
#property (nonatomic, weak) IBOutlet UIImageView *logo;

#Synthesize IBOutlet (UIScrollView) not working

This is about an outlet not synthesising correctly (UIScrollView)
In one particular case, i have an interface declared as:
#interface VC_Create_Preview : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollView;
}
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
And in the implementation....
#synthesize scrollView;
But in debug mode i see that my self->scrollView always has a 0x0 next to it.
Performing operations like
CGRect frame = scrollView.frame;
always returns a origin, height, width of zero (i suspect because it did not synthesise correctly)
Does 0x0 mean that it does not point to any location in memory?
Does this mean that it did not synthesize correctly?
Could it be that there is a broken link somewhere from the outlet to the scrollview in IB?
Cheers
Kevin
Make sure that you actually connected a UIScrollView to the outlet.
Keep in mind that scrollView won't be set until after the view has been loaded from the nib.
0x0 means a null pointer, you probably have not tied the scrollView to a scroll view in IB...This means having the viewControllers nib have a scrollView in its view and tied to the outlet.
If you want to work with outlets and use there real size that user will see (include navigation bar, tab bar, etc.) you must implement all UI changes at viewWillAppear or viewDidAppear.
Read more about stages of loading view from nib.
Try removing local declaration of scrollview i.e.
#interface VC_Create_Preview : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollView;//remove this
}
It may help you. Also while allocating use _scrollView instead of scrollView.
Be sure that you are using the correct class in your code, for example, the first UIViewController you drop into storyboard will have the generic "whateveryourprojectiscalled" UIViewController.
However, the problem for me was that I then had that linked to several other UIViewController, none of which had there own class declared, so the scroller outlet I was writing, is only showing up on the main original UIViewController which is not where I needed it to be. So once I wrote a Custom Class name, and then re wrote the code to accommodate that. The outlet appeared perfectly and worked fine.

Using UISegmentedControl to switch between two views

I created a subclass of UIViewController. In it I have two properties of:
#property (nonatomic, retain) IBOutlet UIView *presetsView;
#property (nonatomic, retain) IBOutlet UIView *customView;
I added a new UIView to my .xib and put some UI elements in it. I want to hide one view, and show the other based on when the UISegmentedControl is pressed.
My question is in Interface Builder, my original View that was provided me by IB, has an outlet connected to it already to File's Owner. Because I have my own two views, presets and custom, how do I make the outlet connections in IB?
I tried deleting the original view that was provided by IB and dragged two new UIViews to the canvas. I then connected an outlet to each. When I push my new viewController, I get an error that there is no view for my viewController. Then when I connect the File's Owner to the "view" outlet that shows up for the view I want to show first, the application runs. I wasn't sure if this was the correct way, and why it would be the correct way. Does the ViewController always need a .view property an outlet to it? Is that why I needed to do this? Thanks.
Write action method for UISegmentedcontrol and implemnt method like below
-(IBAction) selectMessageType {
noResultsPriview.hidden = YES;
//[activityIndicator startAnimating];
switch (msgOptionControl.selectedSegmentIndex) {
case 0:
//code for view1
break;
case 1:
//code for view2 break;
case 2:
//code for view3
break;
case 3;
//code for view4
break;
default:
break;
}
}
Yes every UIViewController has a single root view. It must be able to construct that view when its -loadView method is called either by loading that view from a nib file (and setting its view property as a result) or by creating it programmatically.
In your case leave the view property view alone and don't try to swap that around. This root view will have already have been added to the window and changing the controller's reference to point to some other object will just cause confusion and undefined behavior. Instead add both of your views as subviews of the controller's root view and hide or show then as needed.
UIViewController must have a valid UIView so in your case it can be the original UIView created during the XIB creation. Your two UIView properties are correct. All you have to do is to setup in IB the connection between your presetsView and the original UIView so that you can later switch back to it, then you need to connect customView to your other UIView.
So after you finish your original UIView will have two connections, one as a view of UIViewController (owner) and one as a presentsView.
Those connections are only pointers so that you can use them to manipulate objects in the UIViewController.

Messaging instanciated objects in another class in Objective C

I am using a UITableView within a UIPopover with the aim to set a property to an instantiated object i am using within the original ViewController class(from where the popover is launched). However I cant get access to the declared object(from within the ViewController class). I have tried to import the ViewController class to the popover class, but to no avail, the object just isn't visible within the Popover class. Any guidance on this would be greatly appreciated.
Your popover controller is just a wrapper to place a normal UIViewController in to give it that particular effect. Therefore, to get your variable in scope in the popover controller, you need to set up your UIViewController subclass with a property.
#property (nonatomic, retain) Thing *thing;
So your Main view which is calling the popover will initialize the UIViewController with the property above, and pass it into the init method for the UIPopoverController and proceed as normal.

How to programmatically alter object created in IB

I've placed an image in my UIView with IB.
How do I move this image around, resize it, etc programmatically? I cannot figure out how to reference it from my code.
Thanks a zillion.
You need to make it an IBOutlet.
So in your viewController, or whatever you are loading from the nib, declare it like this:
IBOUtlet UIImageView *myImageView;
Also, in the actual nib, you must connect the File's Owner object to the UIImageView in your file by right-clicking File's owner and dragging the correct outlet name (in this case, myImageView) onto the correct UIImageView instance in your nib.
Now when you load one of the objects using the nib, myImageView will point to the initialized UIImageView, or whatever else you have.
So if you create an UILabel in Inteface Builder, create an outlet like so in your .h file:
IBOutlet UIlabel* somelabel;
Then hook it up in Interface Builder and then you can access it like so:
somelabel.text = #"something";