为通用iOS应用程序创建视图控制器

时间:2013-01-16 19:47:49

标签: ios xcode xib ios-universal-app

更新0

不要回答这个问题。

我想我已经找到了部分问题。

当我尝试重命名使用_创建的自动生成的.xib文件以使用~时,XCode再次中止。 Xcode似乎在中止时至少销毁了一个.xib文件,并且该文件包含应用程序所需的最小视图或窗口对象。虽然缺少的.xib文件已从Finder中删除,但仍会显示在导航器中。

所以现在,我将单独留下_个文件并再试一次,但不要使用我之前提到的建议。

更新0

我的xib更改了以下代码时遇到问题as recommended here. 我怀疑问题来自于将xib名称更改为使用'〜'而不是' _'并且无法修改引用xib名称的相应代码。

问题还可能是我有以下4个文件名可能会让Xcode感到困惑?

BSViewController.h / .m

BSViewController~iPhone.xib

BSViewController〜iPad.xib

原始代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[BSViewController alloc] initWithNibName:@"BSViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[BSViewController alloc] initWithNibName:@"BSViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

修改后的代码(包括额外的)

#import "BSAppDelegate.h"

#import "BSViewController.h"
@implementation BSAppDelegate
@synthesize window;
@synthesize viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[BSViewController alloc] initWithNibName:@"BSViewController" bundle:nil];
    [self.window addSubview:viewController.view];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

BSAppDelegate.h如下。

#import <UIKit/UIKit.h>

@class BSApp;
@class BSViewController;
@interface BSAppDelegate : NSObject <UIApplicationDelegate>{
    UIWindow *window;
    BSViewController *viewController;
}
@property (retain, nonatomic) IBOutlet UIWindow *window;
@property (retain, nonatomic) IBOutlet BSViewController *viewController;
@end

BSViewController.h如下。

#import <UIKit/UIKit.h>

@interface BSViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    UIImage *workingImage;
    IBOutlet UIImageView *imageView;
}

@property (nonatomic, retain) UIImage *workingImage;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;

+ (BOOL)isCameraDeviceAvailable;
- (IBAction) chooseImage:(id) sender;
- (IBAction) grayscale:(id) sender;

@end

BSViewController.m如下。

#import "BSViewController.h"

@interface BSViewController ()

@end

@implementation BSViewController

@synthesize imageView;
@synthesize workingImage;

+(BOOL)isCameraDeviceAvailable
{
    BOOL isCameraAvailable=NO;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] )
            isCameraAvailable = YES;
    }
    return isCameraAvailable;
}

- (IBAction) chooseImage:(id) sender {
    [self.view addSubview: self.imageView];
    UIImage* testCard = [UIImage imageNamed:@"ipad 7D.JPG"];
    CGImageRef num = CGImageCreateWithImageInRect([testCard CGImage],CGRectMake(532, 0, 104, 104));
    UIGraphicsBeginImageContext(CGSizeMake( 250,650));
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextDrawImage(con, CGRectMake(0, 0, 13, 13) ,num);
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(num);
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    self.workingImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageView setImage:self.workingImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.imageView = nil;
}
- (void)dealloc {
    [workingImage release];
    [imageView release];
    [super dealloc];
}

@end

我正在使用Xcode 4.5.2并为iOS 6设计。

在我的BSViewController~iPad.xib我添加了一个&#34; ViewController&#34;在控制点击时产生2个出口的对象:&#34; searchDisplayController&#34;和&#34;查看&#34;。没有&#34;视图&#34;顶级对象;当我试图拖动&#34; +&#34;到'&#34;文件所有者&#34;我没有得到任何结果,可能不应该。

在我的BSViewController~iPhone.xib我已经有一个&#34; Window&#34;对象,并且不知道这是好还是需要添加到~iPad.xib(但我不知道如何)。

0 个答案:

没有答案
相关问题