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";
Related
Why can't I connect outlets from subviews?
Dropped a UIViewcontroller onto storyboard
Dropped a subview (UIView) as a custom class CustomSubView : UIView
Went into Inspector while subview is selected and changed custom class to CustomSubView
I have a couple of labels inside the subview
I tried to drag-drop from storyboard to assistant editor while CustomSubView class header is shown, but I don't get the pop-up to create the outlet. What gives?
I am using Xcode 5.
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.
It was answered here: Cannot create outlet connections to subviews in Interface Builder (Xcode 5)
or you can try this:
After typing the property outlet declarations manually in the customview.h file I could ctrl-drag-connect them from there to their corresponding UIlabel objects in the interfacebuilder. Works only in this direction!
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;
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.
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:
I link the xib with the source code, but when I try to update the UILabel, it can't update with no error msg. So, I make a breakpoint. When I inspect to the UILabel, which is a 0x0.
I already made #synthesize in .m, but I created the IBOutlet on the .h, also I did the
IBOutlet UILabel *xxxLabel;
#property (retain, nonatomic) UILabel *xxxLabel;
already.
I can display the Nib and the default value that I set on the IB. But I can't update it from code... ...I do something like this:
MyController *tmpViewController = [[MyController alloc] initWithNibName:#"MyControllerView" bundle:nil];
[cell addSubview:tmpViewController.view];
and I add it to the cell. But the cell only show the default nib view, instead of a updated one.
I tried to update the background color, it successfully update.
What should I do to do so? Thank you.
It sounds like you didn't link the Outlet to your label in Interface builder.
Just control-click your label, drag it to your "File's Owner" (which should have the class set to "MyController") and select your outlet.