如何在项目中使用XIB连接2个类?

时间:2014-06-10 04:31:09

标签: ios iphone objective-c xcode ipad

我有一个适用于我的iPhone应用程序的课程,我想将另一个带有.h .m .xib文件的课程连接到iPad上。我知道有一些方法只使用1个类和2个xib,但我想要另一种方式。另外我需要在app delegate中编写什么,所以当加载我的应用程序时,它确定它是iPhone还是iPad并选择正确的类。我知道有很多答案,但没有什么对我有用:/现在我有app delegate.h app delegate.m查看controller.h查看controller.m查看controller.xib文件。感谢。

我的appdelegate.h

@class SozdikViewController,iPadViewController;
@interface SozdikAppDelegate : UIResponder <UIApplicationDelegate>{

SozdikViewController *sozdikViewController;

iPadViewController *iPadViewController;

UIViewController *uiViewController;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) UINavigationController *navigationController;
@property(strong,nonatomic) SozdikViewController *sozdikViewController;
@property (strong,nonatomic) iPadViewController *iPadViewController;

@end

appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
  if([[UIScreen mainScreen] bounds].size.height == 480)
{
sozdikViewController=[[SozdikViewController alloc]initWithNibName:@"SozdikViewController" bundle:nil];
        self.window.rootViewController=sozdikViewController;
 }
else if([[UIScreen mainScreen] bounds].size.height == 568)
{
 sozdikViewController=[[SozdikViewController alloc]initWithNibName:@"SozdikViewController" bundle:nil];
         self.window.rootViewController=sozdikViewController;
}
}
else
{
 iPadViewController=[[[iPadViewController] alloc]initWithNibName:@"iPadViewController"bundle:nil];//this line doesn't work
self.window.rootViewController=iPadViewController//incompatible pointer types assigning  to 'UIViewController*'from'iPadViewController*' message appears;
 [self.window makeKeyAndVisible];
return YES;
}

3 个答案:

答案 0 :(得分:0)

希望这会对你有所帮助:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
   //do all your initialisation for the iPad
}
else{
   //do all your initialisation for the iPhone
}

您必须将此代码段放在appDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中。确保在appDelegate中包含这两个类(iPad和iPhone),并在AppDelegate.h文件中创建这两个类的对象。

答案 1 :(得分:0)

UINib *nib;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    nib = [UINib nibWithNibName:@"viewController_iphone" bundle:nil];
else
    nib = [UINib nibWithNibName:@"viewController_ipad" bundle:nil];

但另一种选择是你可以给它命名,就像iphone xib那样你可以命名为viewController.xib和iPad viewController~ipad.xib。所以你不需要写条件。你只需要写UINib *nib = [UINib nibWithNibName:@"viewController" bundle:nil];

希望这会对你有所帮助。

答案 2 :(得分:0)

您可以在AppDelegate中编写以下代码以加载初始屏幕。同样可以在视图控制器中用于推送到另一个屏幕。在这种情况下,我想在应用程序启动后加载LoginViewController ...根据需要修改。

AppDelegate.h声明

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController * navigationcontroller;
@property (strong, nonatomic) LoginViewController *loginViewController;

- (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)
    {


            if([[UIScreen mainScreen] bounds].size.height == 480)
            {
                loginViewController=[[LoginViewController alloc]initWithNibName:@"LoginViewController_iphone4s" bundle:nil];
            }
           else if([[UIScreen mainScreen] bounds].size.height == 568)
            {
                 loginViewController=[[LoginViewController alloc]initWithNibName:@"LoginViewController_iphone5" bundle:nil];

            }


         }

    else
    {
         loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController_ipad" bundle:nil];

    }

    navigationcontroller=[[UINavigationController alloc]initWithRootViewController:loginViewController];

    self.window.rootViewController = navigationcontroller;
    [ navigationcontroller setNavigationBarHidden:YES animated:YES];

    [self.window makeKeyAndVisible];
    return YES;
}

希望它能帮助你......!