Question

I am new to the iPhone SDK and am trying to create 3 views and switch between them. Data will come from a server and I will basically be showing 1 view and caching the other two. So far I am just trying to create a view and display it at run-time. My code is listed below. It shows only a blank screen and I think I am missing a key concept. Any Help?

#import <UIKit/UIKit.h>
#import "ImageViewController.h"
@interface Test5ViewController : UIViewController 
{
    IBOutlet UIView *rootView;
    ImageViewController *curImage;
    ImageViewController *nextImage;
    ImageViewController *prevImage;
}

@property(nonatomic,retain) IBOutlet UIView *rootView;
@property(nonatomic,retain) ImageViewController *curImage;
@property(nonatomic,retain) ImageViewController *nextImage;
@property(nonatomic,retain) ImageViewController *prevImage;

@end

and

- (void)loadView 
{

    self.curImage = [[ImageViewController alloc]initWithNibName:@"ImageView" bundle:[NSBundle mainBundle]];
    UIImage *pic = [UIImage imageNamed:@"baby-gorilla.jpg"];
    [self.curImage assignImage:pic];
    self.rootView = self.curImage.view;

}

and

#import <UIKit/UIKit.h>


@interface ImageViewController : UIViewController 
{
    IBOutlet UIImageView *image;
}

-(void)assignImage:(UIImage *)screenShotToSet;


@property(nonatomic,retain) IBOutlet UIImageView *image;

@end
Was it helpful?

Solution

Welcome to the iPhone SDK!

In general, there are two ways to get any view displayed.

First, and most commonly, you use a NIB file created by the Interface Builder. This is usually the easiest way to get started and I would recommend it for what you're trying to do here. It's too lengthy to describe all the steps you need to do for what you have here, but basically start in xcode by creating a new file and selecting "user interfaces" and choose View XIB. This will create a basic NIB file (they're called NIBs rather than XIBs for historical reasons). The first step in interface builder is to change the class name of the "File's Owner" to your UIViewController subclass (Test5ViewController). You can then drop anything that IB will allow into the view window or even replace the pre-supplied view object with one of your own. And here's the trick: make sure the view outlet (supplied by the UIViewController superclass) is connected to a view. Once this is done, this view will be automatically loaded when your NIB is loaded. You can then just put your UIViewController subclass (Test5ViewController) in your MainWindow.xib NIB file to get it automatically loaded, and you're in business.

Now, the way you're doing it here is the second way. Some people like to code this way all the time and not user interface builder. And while it's definitely necessary sometimes and always more flexible, it makes you understand what is happening a bit better. There may be other things, but the main thing you're missing is that in your code above, you have nothing that is adding your view into the view hierarchy. You need to check first that you have an UIApplicationDelegate subclass and it needs to load your "root" UIViewController class. All initial project creation types in xcode do this (except Window-based application). It is code like:

[window addSubview:rootController.view];

Once this is done, if your view controller wasn't loaded by the NIB (described briefly above), your loadView method will be called, expecting you to build your own view hierarchy. Above, you created the view(s), but failed to put them in a hierarchy. You need something like:

[self.view addSubview:curImage.view];

No view will be rendered until added to the view hierarchy. Make sure to look up the UIView class in the documentation and understand the variety of ways to add and remove views to the view hierarchy.

A couple things I should warn you about: * your code above is leaking. You need to review how objective-C properties work. There's lots on this site about it. More than I have time to write about here. * don't create a rootView property in the case you have here. There already is one in the superclass (UIViewController). It's just 'view'. Use that for saving your root view.

I hope this helps you get started. It can be bewildering at first, but you'll soon get it going! I recommend building and rewriting and rebuilding a lot of sample code before you do your "real" application. The SDK has many great samples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top