Ipad方向问题 - UITextView重新加载和键盘消失

时间:2011-07-07 08:52:34

标签: iphone ipad orientation uitextview

直到我在模拟器上运行我的应用程序一切正常。包括所有视图的方向。现在,当我在Ipad上测试应用程序时,我发现了一个我认为必须处理的问题 我有一个 UITableView ,每行都有 UILabel,UITextView和UIButton 。我面临的问题是,当我在textview上输入内容时,在更改方向之间,文本会随键盘一起消失。虽然我知道这个的原因是我每次改变方向时都会重新加载tableview,但是我无法处理它。重新加载tableview对于正确定位其他视图也很重要 需要做什么?如果需要代码,我会更新我的问题。

Nitish

更新

- (void)viewDidLoad
{
    [super viewDidLoad];
    checkTextView = FALSE;
    self.title=@"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 个答案:

答案 0 :(得分:1)

一个解决方案(可能不是最好的)是在旋转发生之前保存正在编辑的行的文本和indexPath。旋转完成后,您只需恢复文本和焦点。在tableview控制器中使用以下消息:

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

如果未调用这些消息,请参阅here