Ios 如何在我的项目中将2个类与XIB连接起来?
我有一个用于iPhone应用程序的类,我想将另一个带有.h.m.xib文件的类连接到iPad上。我知道有一些方法可以只使用1类和2个xib,但我想要另一种方法。另外,我需要在应用程序委托中编写什么,以便在加载我的应用程序时确定它是iPhone还是iPad,并选择正确的类。我知道有很多答案,但对我来说什么都不管用:/n现在我有app delegate.h app delegate.m view controller.h view controller.m view controller.xib文件。谢谢 我的appdelegate.hIos 如何在我的项目中将2个类与XIB连接起来?,ios,iphone,objective-c,xcode,ipad,Ios,Iphone,Objective C,Xcode,Ipad,我有一个用于iPhone应用程序的类,我想将另一个带有.h.m.xib文件的类连接到iPad上。我知道有一些方法可以只使用1类和2个xib,但我想要另一种方法。另外,我需要在应用程序委托中编写什么,以便在加载我的应用程序时确定它是iPhone还是iPad,并选择正确的类。我知道有很多答案,但对我来说什么都不管用:/n现在我有app delegate.h app delegate.m view controller.h view controller.m view controller.xib文件
@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
希望这将帮助您:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
//do all your initialisation for the iPad
}
else{
//do all your initialisation for the iPhone
}
您必须将这段代码放在appDelegate.m中的-BOOLapplication:UIApplication*application didfishlaunchingwithoptions:NSDictionary*launchOptions方法中。确保在appDelegate中同时包含iPad和iPhone两个类,并在appDelegate.h文件中创建这两个类的对象
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]
希望这将对您有所帮助。您可以在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;
}
希望它能帮助你 谢谢。但是你难道不知道为什么我的应用程序代理没有看到我创建的第二个类吗。看,我在.h文件中声明了两个类@属性强的非原子iPadViewController iPadViewController和SozdikVieController相同;第一个是:sozdikViewController,第二个是iPadViewController。在.m文件中,当我编写self.window.rootViewController=sozdikViewController时,它工作正常。但是当我写self.window.rootViewController=iPadViewController时。出现“iPadViewController*”消息中指定“UIViewController”的不兼容指针类型。您需要将iPadViewController创建为UIViewController的子类。将应用程序代理代码放入问题中..问题在于iPadViewController..将其删除,然后使用相同或不同的名称重新创建。请记住,它应该是UIViewController的子类。。!将appdelegate.m的代码放在您的问题下方..您尝试了什么。。!检查是否在AppDelegate.h中导入了SozdikViewController、iPadViewController…就像导入SozdikViewController.h导入iPadViewController.hok一样,我刚刚导入了它们,但仍然不起作用。还有其他想法吗?也在AppDelegate.m导入它们,并检查..已完成。由于某些原因,仍然是AppDelegate.m未看到iPadViewController类。
@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;
}