将UIImageView添加到rootViewController

时间:2013-07-19 21:50:33

标签: ios objective-c

我只是想做的就是在我开始使用iOS时在屏幕上显示图像 发展。我想,因为UIImageViewUIView的子类,我会以类似的方式添加它,但我没有运气。我知道这是一个简单的问题,但任何帮助都会受到赞赏。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:                 (NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.rootViewController = [UIViewController new];
    UIImage* stallion = [UIImage imageNamed:@"stallion1.png"];

    UIImageView* iv = [UIImageView alloc];
    iv.image = stallion;
    [self.window.rootViewController.view addSubview:iv];
    [self.window makeKeyAndVisible];

    return YES;
}

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

App代表:

#import "RootViewController.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    //allocate and initialize the root view controller
    RootViewController *rootViewController = [[RootViewController alloc] init];

    //set the root view controller
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];

    return YES;
}

创建一个名为RootViewController的UIViewController的子类

·H

#import <UIKit/UIKit.h>

//set the class RootViewController as a subcalss of UIViewController
@interface RootViewController : UIViewController

@end

的.m

//this is one of the life cycle methods of a UIViewController and should already be in the code when the class is created
- (void)viewDidLoad
{
    //execute the viewDidLoad method of the superclass (UIViewController)
    [super viewDidLoad];

    //allocate and initialize the image view 
    //assign the image view a frame
         //x offset from the left = 0.0
         //y offset from the top = 0.0
         //width = the view controller's view's width (should be the whole screen)
         //height = the view controller's view's height(should be the whole screen)
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
    [iv setImage:[UIImage imageNamed:@"myImageName.png"]];

    //the background will be red if everything is setup correctly, but the image isn't found
    [iv setBackgroundColor:[UIColor redColor]];

    //add the image view to the view controller's view 
    [self.view addSubview:iv];
}

答案 1 :(得分:0)

//修改

如果您将阅读有关视图控制器信息的书籍,那将对您非常有帮助。这是非常重要的信息。我建议你阅读Erica Sadum的“The Core IOS 6 Cookbook”。它真的帮助了我。

您还可以查看Apple文档http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/

在AppDelegate.m中写下这样的内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    MyViewController *viewController = [[MyViewController alloc] init];

    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

接下来,您必须添加UIImageView,但仅在加载创建的视图控制器的视图时。最好的方法是viewDidLoad。您还可以使用viewDidAppear:或viewWillAppear:。 您必须在MyViewController类中重写此方法(它应该从UIViewController类继承)。

 - (void)viewDidLoad
{
  [super viewDidLoad];
  UIImageView *imageView = [UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.heigth);
  imageView.image = [UIImage imageNamed@"stallion1.png"];
  [self.view addSubview:imageView];
}