Ipad orientation issue - UITextView reloads and keyboard disappears
While I was running my application on simulator everything was working fine. Including the orientation of all views. Now, when I tested the application on my Ipad, I noticed an issue which I think I must take care of.
I have a UITableView which has a UILabel, UITextView and a UIButton in each of its row. The problem I am facing is, when I type something on the textview, and in between change the orientation, the text disappears along with the keyboard. Although I know the reason for this is the reloading of tableview which I do every time I change the orientation, I am not able to handle it. Reloading of tableview is also important for other views to be oriented properly.
What needs to be done?
Update
- (void)viewDidLoad
{
[super viewDidLoad];
checkTextView = FALSE;
self.title=@"Tasks";
arrTextViews = [[NSMutableArray alloc]init];
[self checkOrientation];
// Do any ad开发者_如何学Cditional 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;
}
One solution (maybe not the best) would be to save the text and the indexPath of the row being edited just before the rotation occurs. When the rotation is finished, you'll just have to restore the text and the focus. Use the following messages in your tableview controller:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
See here, if these messages are not called.
精彩评论