2011-07-07 31 views
0

直到我在模擬器上運行我的應用程序一切工作正常。包括所有視圖的方向。現在,當我在Ipad上測試應用程序時,我注意到了一個我認爲必須處理的問題。
我有一個UITableView它有一個UILabel,UITextView和UIButton在其每行中。我面臨的問題是,當我在textview上鍵入內容時,以及在兩者之間改變方向時,文本隨鍵盤一起消失。雖然我知道這樣做的原因是重新加載tableview,我每次都會改變方向,但我無法處理它。重新加載tableview對於其他視圖的正確定位也很重要。
需要做什麼?如果代碼是必需的,我會更新我的問題。Ipad方向問題 - UITextView重新加載和鍵盤消失

尼蒂什

更新

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    checkTextView = FALSE; 
    [email protected]"Tasks"; 
    arrTextViews = [[NSMutableArray alloc]init]; 
    [self checkOrientation]; 
    // Do any additional setup after loading the view from its nib. 

} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

} 

-(void)viewWillDisappear:(BOOL)animated 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; 

} 

-(void) checkOrientation 
{ 
    UIInterfaceOrientation orientation = self.interfaceOrientation; 

    @try 
    {   

     if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { 

      textXX=340.0; 
      btnXX=900.0; 
      textLL=480.0; 

      [commentsTable reloadData]; 

     } 
     else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 

      textXX=240.0; 
      btnXX=650.0; 
      textLL=350.0; 

      [commentsTable reloadData]; 

     }  
    } 
    @catch (NSException *ex) { 
     [Utils LogExceptionOnServer:@"TodayViewController" methodName:@"checkOrientation" exception:[ex description]]; 
    } 

} 



- (void)receivedRotate:(NSNotification *)notification 
{  
    [self checkOrientation]; 
} 


// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [arrComments count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 

    } 
    cell.selectionStyle=UITableViewCellSelectionStyleNone; 

    // Configure the cell 
    NSArray *arrSeparate = [strName componentsSeparatedByString:@":"]; 
    NSString *str1 = [arrSeparate objectAtIndex:0]; 
    NSString *str2 = [arrSeparate objectAtIndex:1]; 

    lblName1=[[UILabel alloc]init]; 
    lblName1.frame=CGRectMake(10, 13, 200, 30); 
    lblName1.numberOfLines=1; 
    [lblName1 setText:str1]; 
    [lblName1 setTextColor:[UIColor colorWithRed:0.0/255.0 green:73.0/255.0 blue:121.0/255.0 alpha:1.0]]; 
    [lblName1 setFont:[UIFont boldSystemFontOfSize:25.0]]; 
    [lblName1 setBackgroundColor:[UIColor clearColor]]; 
    [cell.contentView addSubview:lblName1]; 
    [lblName1 release]; 

    lblName2=[[UILabel alloc]init]; 
    lblName2.frame=CGRectMake(10, 50, 200.0, 70); 
    lblName2.numberOfLines=2; 
    [lblName2 setText:str2]; 
    [lblName2 setTextColor:[UIColor colorWithRed:0.0/255.0 green:73.0/255.0 blue:121.0/255.0 alpha:1.0]]; 
    [lblName2 setFont:[UIFont boldSystemFontOfSize:25.0]]; 
    [lblName2 setBackgroundColor:[UIColor clearColor]]; 
    [cell.contentView addSubview:lblName2]; 
    [lblName2 release]; 

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(textXX-1, 12, textLL+2, 132)]; 
    imgView.image = [UIImage imageNamed:@"box.png"]; 
    [cell.contentView addSubview:imgView]; 
    [imgView release]; 

    //txtComment 
    txtComment = [[UITextView alloc] initWithFrame:CGRectMake(textXX,13, textLL, 130)]; 
    txtComment.scrollEnabled = YES; 
    txtComment.userInteractionEnabled = YES; 
    txtComment.tag=indexPath.row; 
    iTextViewTag = txtComment.tag; 
    strGetText = txtComment.text; 
    [txtComment setTextColor:[UIColor colorWithRed:0.0/255.0 green:73.0/255.0 blue:121.0/255.0 alpha:1.0]]; 

    [txtComment setFont:[ UIFont boldSystemFontOfSize: 25 ]]; 
    [cell.contentView addSubview:txtComment]; 
    [arrTextViews addObject:txtComment]; 
    [txtComment release]; 

    UIImageView *imgBtn = [[UIImageView alloc] initWithFrame:CGRectMake(btnXX-1, 12, 72, 132)]; 
    imgBtn.image = [UIImage imageNamed:@"commentbbtnbox.png"]; 
    [cell.contentView addSubview:imgBtn]; 
    [imgBtn release]; 

    //btnClose 
    btnClose = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btnClose.frame=CGRectMake(btnXX, 13,70,130); 
    btnClose.tag= indexPath.row; 
    btnClose.backgroundColor = [UIColor grayColor]; 
    [btnClose addTarget:self action:@selector(btnCloseAction:) forControlEvents:UIControlEventTouchDown]; 
    [cell.contentView addSubview:btnClose]; 

    return cell; 
} 

回答

1

一個解決方案(也許不是最好的)是保存的文本,該行的indexPath旋轉發生之前正在編輯。旋轉完成後,您只需恢復文本和焦點。使用下面的消息你的tableview控制器:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration 
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

here,如果這些消息不會被調用。

+0

我應該在哪裏得到textview的文本?在tableview中或在一些委託方法或在viewDidLoad? – Nitish

+0

willAnimateRotationToInterfaceOrientation是一個UIViewController消息,您可以在管理您的tableview的ViewController中重寫該消息。它應該在旋轉之前被調用。你應該添加保存文本所需的代碼。如果您需要更多幫助,請提供一些代碼(即tableView:cellForRowAtIndexPath:和viewDidLoad的內容) – FKDev

+0

我已更新我的問題。 – Nitish