2

我只是想做的只是在我刚刚开始 iOS 开发时在屏幕上显示一个图像。我想既然UIImageView是 的子类UIView,我会以类似的方式添加它,但我没有任何运气。我知道这是一个简单的问题,但任何帮助将不胜感激。

- (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;
}
4

2 回答 2

1

尝试这样的事情:

应用委托:

#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];
}
于 2013-07-19T22:02:23.303 回答
0

//编辑

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

您也可以只查看苹果文档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];
}
于 2013-07-19T22:18:18.320 回答