UIView不会旋转

时间:2009-11-28 18:47:49

标签: iphone objective-c

我在UIViewController中有以下代码

Notepad *notepad = [[Notepad alloc] initForNewTopLevelTask:0 andDAO:self.dao];

[self.navigationController pushViewController:notepad animated:YES];
[notepad release];

initForNewTopLevelTask​​:andDAO:方法是:

- (id) initForNewTopLevelTask:(int) theTableSize andDAO:(DAO*) aDAO {
    self.dao = aDAO;
    tableSize = [[NSNumber alloc] initWithInt:theTableSize];
    self.isNew = [NSNumber numberWithBool:YES];
    self.isSubtask =[NSNumber numberWithBool:NO];
        return self;
}

当我旋转视图时没有任何反应,它不会旋转。如果我将UIViewController行更改为:

Notepad *notepad = [[Notepad alloc] init];

它旋转得很好!

视图不是标签控制器的一部分,我已经实现了:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
}

1 个答案:

答案 0 :(得分:1)

您应该始终在自己的init方法中调用父类init方法。至少从NSObject扩展的任何对象。

请参阅http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html#//apple_ref/doc/uid/TP30001163-CH22-SW4

将您的初始化函数更改为:

- (id) initForNewTopLevelTask:(int) theTableSize andDAO:(DAO*) aDAO {
    if ( self = [super init] ) {
    self.dao = aDAO;
    tableSize = [[NSNumber alloc] initWithInt:theTableSize];
    self.isNew = [NSNumber numberWithBool:YES];
    self.isSubtask =[NSNumber numberWithBool:NO];
    }
    return self;
}