How can I make UIImageView change its image programmatically? - iphone

I have this code:
- (void)viewDidLoad {
[super viewDidLoad];
landscape.image = [UIImage imageNamed:#"tenerife1.png"];
}
First, I created an UIImageView with IBOutlet. Landscape is pointing to that UIImageView. In InterfaceBuilder I told to show an specific image in that UIImageView. It never changes. I always see the image I specified in InterfaceBuilder. But I want to set it programmatically. How can I do this?

Try calling setNeedsDisplay on the view after you change the image:
[landscape setNeedsDisplay];

Add this code, it might help you:
-(void)viewDidAppear:(BOOL)animated
{
landscape.image = [UIImage imageNamed:#"tenerife1.png"];
}

is it possible you did not have a custom view set up for that nib file? I had the same problem. I created a UIview subclass for that view controller, went into Interface Builder and set the view to that UIView subclass and then added an import header for the UIView into the Controller class. thats what did it for me

Don't forget about connection between imageview and File's Owner in InterfaceBuilder.
it's only you need after that:
landscape.image = [UIImage imageNamed:#"tenerife1.png"];

Related

How to build a long interface using interface builder

I build an UIViewController, that higher than iPhone screen ( the height is 900px ), i build it using UIScrollView.
In the Interface Builder, how to put an View in the bottom part of the UIScrollview ? i've tried using freeform in the inspector -> Simulated Metrics, but every time i move back to the ipad/iphone screensize, the views are mess. And when i ran it in the simulator, the view still a mess
I'd add a view programmatically and make it subview for the scrollview, i don't know how to do it using IB.
If you want to try programmatically can try something like:
UIView*myView=[[UIView alloc ] initWithFrame:CGRectMake(0, 480, 320, 200)];
[myView setBackgroundColor:[UIColor redColor]]; // Just for testing purpose
[scrollView addSubview:myView];
You have to do a bit of it programatically I'm afraid but you can do most of it in a xib.
Your xib would contain a UIView that's your normal view, containing your scrollview.
It would also contain another UIView; this will be as long as you want and will be your scrollview's contents.
In your .h file, have a property like this and attach it to the second UIView in your xib
#property (nonatomic, retain) IBOutlet UIView *scrollingContents;
and in your viewDidLoad, that's where you attach the contents inside the scroll view
- (void)viewDidLoad {
[super viewDidLoad];
[self.scrollView addSubview:scrollingContents];
self.scrollView.contentSize = scrollingContents.bounds.size;
}

How to call a UIView from a UIViewController

I need to create a transparent UIView with an UIActivityIndicator and an UIImage on it.
I found a code from a tutorial as in how to create the Round edge UIView.
Can someone help me call this UIView from my UIViewController class. (To call a UIViewConroller from another UIViewController we use the following code;)
ViewOne *v1 = [ViewOne alloc] initWithNibName:nil bundle:nil];
[self.navigationController v1 animated:YES];
but this doesn't work when calling UIView's. So how can i call it ?
To add an UIImage and a UIActivity indicator programatically to the UIView ?
Every view controller has a view property. Just use UIView *yourView = yourViewController.view to get it. From there you can use [yourView addSubView:someOtherView] to add the activity indicator and the image, as well as setting the transparency.
wouldn't you just wire up the UIView to an IBOutlet, and then just add your programmatically created view to the outlet

how to show same image in all view without implementing it in all view?

I want to show my apps logo image in top of every view. But I want to create it once in a view that show it in every view like master page in ASP.net. please some one help me.
Create a subclass of UIViewController, and in viewDidLoad method, get and place the image where your want :
.h
#interface MyLogoViewController : UIViewController
// your interface
#end
.m
#implementation MyLogoViewController
- (void) viewDidLoad {
UIImage* image = [UIImage imageNamed:#"image.png"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame.origin.x = // whatever you want
imageView.frame.origin.y = // whatever you want
[self.view addSubview:imageView];
[self.view bringSubviewToFront:imageView];
[imageView release];
}
#end
Then for any of your view controller that controls a view in wich you want that image to be displayed should inherit that subclassed View controller instead of UIViewController.
#interface MyNewViewController : MyLogoViewController {
// your interface
}
#end
That way, image displays automatically without having to do anything special.
If you plan to add subviews programmatically in your view controllers, then keep a reference for imageView into MyLogoViewController class as a member var (instead of just creating / releasing it on the fly), so you will be able to put your new views behind the existing logo if needed.
you can place that image in main.window. so you can see that image in every view which views are having subviews to main window. But you need fix all view sizes according to your images size fixed in main window.Thats enough.
You can also add the image to window and have fix the size of the view then u can see in sub view
YOu can add that view in window and add other view below that window image. so you can see that in every page.
Create a modal class for the image and call this from viewDidLoad of every controller.

Create a custom UIView as a tableHeaderView programmatically

I want to create a 'detail view' in my navigation-based app similar to the address book app.
I want to create a UIView that has an UIImageView and a UILabel that I can pass to a UITableVIew's tableHeaderView property when pushed by a navigation controller. The label text and image must take information from the managed object context when it loads.
I started trying to do this using IB, but go fed up when the label text wouldn't update, regardless of where I put the myView.UILabel.text = #"some new text". It just kept presenting the text I entered in the IB inspector window.
So without using IB, how would I go about creating a UIView that has a UILabel and a UIImageView in it?
Should I be creating a new class that is a sub-class of UIViewController or just UIView?
How do I go about creating the sub-views for the label and image?
Where should the text and image URL be set in code? (viewDidLoad or loadView or viewWillLoad)(UIView, UIViewController, detailViewController)?
If you started using IB and the UILabel text wouldn't update, sounds like the IBOutlet of your view controller isn't correctly connected to the label in IB.
Hopefully that will fix your problem with using IB. And in the viewController code, you should update that text in viewDidLoad.
But if you want to do this programmatically, I would make a subclass of UIView (not UIViewController).
And I would override initWithFrame and do all the setup there.
Something like the following:
myView.m
#implementation myView
#synthesize imageView, myLabel;
- (id) initWithFrame:(CGRect) frame
{
if (self = [super initWithFrame:frame]) {
// Setup your image view and add it to the view.
self.imageView = [[[UIImageView alloc] initWithFrame:initWithFrame:frame] autorelease];
self.imageView.image = ...
[self addSubview:self.imageView];
// Setup your label
self.myLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
self.myLabel.text = #"whatever you like";
[self addSubview:self.myLabel];
}
return self;
}
Make sure to clean up memory properly in the dealloc method of course depending on whether you like make class properties vs class variables.

iPhone - UIViewController not rotating when device orientation changes

I have got my own custom UIViewController, which contains a UIScrollView with an UIImageView as it's subview. I would like to make the image to auto rotate when device orientation changes, but it doesn't seem to be working...
In the header file, I've got;
#interface MyViewController : UIViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *containerView;
UIImageView *imageView;
}
These components are initialised in the loadView function as below;
containerView = [[UIScrollView alloc] initWithFrame:frame];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://..."]];
UIImage *image = [[UIImage alloc] initWithData:data];
imageView = [[UIImageView alloc] initWithImage:image];
[image release];
[containerView addSubview:imageView];
And I have added the following method, assuming that's all I need to make the view auto-rotate...
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
MyViewController loads fine with the image I've specified to grab from the URL, and the shouldAutorotate... function is being called, with the correct UIInterfaceOrientation, when I flip the device too.
However, didRotateFromInterfaceOrientation method do not get called, and the image doesn't seem to rotate itself...
Could someone please point out what I need to add, or what I have done wrong here?
Thanks in advance!
This may not be the right answer for you, because you don't specify the context that the UIViewController's in, but I just found an important gotcha in the Apple documentation that explains the similar problem I'm having.
Tab bar controllers support a portrait
orientation by default and do not
rotate to a landscape orientation
unless all of the root view
controllers support such an
orientation. When a device orientation
change occurs, the tab bar controller
queries its array of view controllers.
If any one of them does not support
the orientation, the tab bar
controller does not change its
orientation.
I've noticed that there are issues when rotating a UIView that's not the first or only view as a direct child of the main window.
So if your UIView is part of a Navigation Controller or a Tab View Controller, you'll also need to override shouldAutoRotateToInterfaceOrientation on the Navigation Controller or Tab View Controller.
Also: using [UIApplication setStatusBarOrientation] helps to work around things if/when you need to do it manually.
To make this kind of thing work in my application, I had to override
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
[self layoutSubviews];
}
and also layoutSubviews
- (void)layoutSubviews
{
NSLog(#"layoutSubviews called");
...recalc rects etc based on the new self.view.bounds...
}
I'm not sure that this is absolutely required, but it worked for me.
Sometimes, if you add a subview to a view, it's your responsibility to make sure that the methods are passed to the subview; a couple of days ago I wrote a short post about this. For example, if you have a UIViewController and add a UINavigationController as subview, you must add this code to the UIViewController if you want viewWillAppear:animated: to be called when UINavigationController.view appears:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[projectNavigationController viewWillAppear:animated];
}
It might be the case that the willRotateToInterfaceOrientation and didRotateFromInterfaceOrientation method also need to be called by the superview; I am not really sure about this, but give it a try.
This is discussed in Apple Technical Q&A QA1688.
Sometimes if you stack multiple views on top of each other for some reason, the anotherController might not receive rotation event.
[myWindow addSubview:primaryViewController.view];
[myWindow addSubview:anotherController.view];
A lazy way (not a good design) to fix this is only add one subview on window, but initialize multiple controller on the app delegate. Then when you need to switch window, remove the current view and add the view you want
[self.view removeFromSuperview];
AppDelegate *dg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[dg window] addSubview:[[dg viewController] view]];
I just came across this having a similar problem. I have a series of view controllers/complex views, that all rotate perfectly and couldn't figure out why the new one I just added on wasn't rotating. After a LOT of trial and error, the reason was that I wasn't calling the init method (it's the standard init method) when allocating the view controller;
e.g. I was doing
m_timerViewController = [TimerViewController alloc];
instead of
m_timerViewController = [[TimerViewController alloc] init];
To expand on jonoogle's post. I had a similar error. My view has a nib and my custom init:
- (id)initWithCategory:(Category *)category inManagedObjectContext:context{
didn't include the call to init the nib.
self = [super initWithNibName:nil bundle:nil];
Adding that line made my view rotate like it is supposed to.
I copied this from this link
And it works for me.... Reason why i have added this here is to make it easy for others to find. It took me many hours to find this fix:
Make a new set of class files of the UIViewController type, go into the .h file of this class and change this line
#implementation MyTabBarController: UIViewController {}
#end
to something like this
#implementation MyTabBarController: UITabBarController{
}
Now go into the nib file and click on the UITabBarController object and go to it's identity tab, and make it Class MyTabBarController.
now in MyTabBarController.m make sure this is in it.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)io {
return YES;
}
You can probably get rid of everything else in there if you want.
just do this if you what to rotate from landscape to portrait!
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}