Ios 如何在iphone的同一视图控制器中放置两个xib
我正在尝试为同一个视图控制器制作两个xib。我有一些代码,但我不知道根据代码,视图名称是什么。这是我必须放入视图的代码吗 这是我的密码:Ios 如何在iphone的同一视图控制器中放置两个xib,ios,iphone,objective-c,Ios,Iphone,Objective C,我正在尝试为同一个视图控制器制作两个xib。我有一些代码,但我不知道根据代码,视图名称是什么。这是我必须放入视图的代码吗 这是我的密码: if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) { NSLog(@"Ipad "); } else { NSLog(@"Iphone %f ",[[UIScreen mainScreen] bounds].size.height); if ([[UIScreen
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
NSLog(@"Ipad ");
}
else
{
NSLog(@"Iphone %f ",[[UIScreen mainScreen] bounds].size.height);
if ([[UIScreen mainScreen] bounds].size.height == 568) {
viewName=[[NSString alloc]initWithString:@"ViewControllerIphone5"];
//this is iphone 5 xib
} else {
viewName=[[NSString alloc]initWithString:@"ViewController"];
// this is iphone 4 xib
}
}
请帮忙
提前谢谢我已经这样做了
#define IS_IPHONE_5 (CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size, CGSizeMake(640, 1136)))
UIViewController *homeViewController;
if (IS_IPHONE_5) {
homeViewController = [[LGHomeViewController alloc] initWithNibName:@"LGHomeViewController" bundle:nil];
} else {
homeViewController = [[LGHomeViewController alloc] initWithNibName:@"LGHomeViewController_IPhone4" bundle:nil];
}
请尝试此代码
#define IDIOM UI_USER_INTERFACE_IDIOM()
#define IPAD UIUserInterfaceIdiomPad
#define IPHONE UIUserInterfaceIdiomPhone
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
NSLog(@"Ipad ");
}
else
{
// The device is an iPhone or iPod touch.
YourViewController *yourVC;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
yourVC = [[YourViewController alloc] initWithNibName:@"YourViewController_iPhone5" bundle:nil];
}
else
{
yourVC= [[YourViewController alloc] initWithNibName:@"YourViewController_iPhone4" bundle:nil];
}
}
您需要在init上加载正确的NIB,而不是viewDidLoad。这应该可以做到,但正如其他人指出的那样,自动布局通常是一种方式
- (id)init
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
return [self initWithNibName:@"ViewControllerIpad" bundle:nil];
}
else if ([[UIScreen mainScreen] bounds].size.height == 568)
{
return [self initWithNibName:@"ViewControllerIphone5" bundle:nil];
}
else
{
return [self initWithNibName:@"ViewController" bundle:nil];
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
6401136?你确定吗?是的,我是:)它的比率so 568*2=计算你的自我:Pi将此代码和调试通过断点,它将转到iphone4,但与以前一样,如果我以4英寸的速度运行应用程序,它的运行非常完美,但在3.5中,所有图像的位置都发生了变化,在实现代码后,没有发现任何差异。我将xib连接到我的viewcontroller:(然后是关于您的xib布局问题,您是使用autolayout还是spring Struts是的,我使用autolayout为什么不使用autolayout格式,它支持所有大小的screens@Rahul更好的解决方案是在这种情况下使用自动布局,无需管理2 xib。我使用了您的方法,但没有调用我的homeviewcontroller_iphone4.xib。即使在将其记录到该xib。如果是,则为iPhone4和5添加不同的xib,与文件所有者中同一类下的iPhone4 xib不同???(init)在创建视图控制器时不被调用,只需调用“MyViewController*MyViewController=[[MyViewController alloc]init];”即可加载正确的NIB。
- (id)init
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
return [self initWithNibName:@"ViewControllerIpad" bundle:nil];
}
else if ([[UIScreen mainScreen] bounds].size.height == 568)
{
return [self initWithNibName:@"ViewControllerIphone5" bundle:nil];
}
else
{
return [self initWithNibName:@"ViewController" bundle:nil];
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}