我想以编程方式创建一个表,并根据更改方向进行更改。问题在于,当我启动应用程序时,无论方向如何,表总是出现在同一位置。
并且我没有说明为什么第一次调用updateViewOnRotate方法3次。
代码全部在一个类UIViewController中。
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateViewOnRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[super viewDidLoad];
}
-(void) updateViewOnRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) {
// present the other viewController, it's only viewable in landscape
[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
// get rid of the landscape controller
UIView *opcionesView = [[[UIView alloc] initWithFrame:CGRectMake(100, 100, 250, self.view.frame.size.height)] autorelease];
opcionesView.backgroundColor = UIColor.redColor;
UITableView *table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 50, 250, 500) style:UITableViewStylePlain] autorelease];
[opcionesView addSubview:table];
[self.view addSubview:opcionesView];
[self dismissModalViewControllerAnimated:YES];
}
else{
[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
UIView *opcionesView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, self.view.frame.size.height)] autorelease];
opcionesView.backgroundColor = UIColor.redColor;
UITableView *table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 50, 250, 500) style:UITableViewStylePlain] autorelease];
[opcionesView addSubview:table];
// UINavigationBar *b=[[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 250, 50)]autorelease];
// [opcionesView addSubview:b];
[self.view addSubview:opcionesView];
}
}
答案 0 :(得分:0)
我还不能发表评论。如果可以,那就是一个。
为什么不使用 - (void)willAnimateRotationToInterfaceOrientation或其他视图控制器回调来执行此操作?也许它会更好。