0

我想以编程方式创建一个表并根据更改方向对其进行更改。问题是,当我启动应用程序时,无论方向如何,表格总是出现在同一位置。

我不明白为什么方法 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];

}  

  }
4

1 回答 1

0

我还不能发表评论。如果可以的话,这将是一个。

为什么不使用 - (void)willAnimateRotationToInterfaceOrientation 或其他视图控制器回调来执行此操作?也许它会更好地工作。

于 2011-11-18T13:43:23.623 回答