JingJingTao
JingJingTao

Reputation: 1790

iOS iphone uialertview in landscape adds another textview

I am currently using an alert view with a textview inside it, but something strange happens when I rotate the iPhone to landscape, another textview seems to appear on the alert view. This issue does not happen on the iPad. Does anyone know what could be causing this? Any advice or help would be greatly appreciated.

My code is placed in the app delegate and is listed below,

- (IBAction) loadAboutUs 
{
UIAlertView *showSelection;
showSelection.delegate = self;
NSString *key = [NSString stringWithFormat: @"Drug Tariff\n"
@"Version: %@\n"
@"text\n\n"
@"text\n"
@"text\n"
@"text", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
showSelection = [[UIAlertView alloc] 
                 initWithTitle: @""
                 message: @"\n\n\n\n\n\n\n\n\n\n"//The new lines are used to increase the size of the pop-up
                 delegate: nil 
                 cancelButtonTitle: @"OK" 
                 otherButtonTitles: nil];

//The text view inserted into the alertview 
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(12.0, 15.0, 260.0, 210.0)];
myTextView.text = key;
myTextView.dataDetectorTypes = UIDataDetectorTypeAll;//set the textview to pick up all link types
[myTextView setFont:[UIFont fontWithName:@"ArialMT" size:16]];
myTextView.textAlignment = UITextAlignmentCenter; 
myTextView.editable = false;//the textview needs to be uneditable for the dataDectector to work
myTextView.scrollEnabled = true;
[myTextView setBackgroundColor:[UIColor whiteColor]];
[showSelection addSubview:myTextView];

[showSelection show];
}

I've attached an image of the issue,

Alertview issue

UPDATE:

the output I get when I run the code below,

NSArray *subviews = [showSelection subviews];
for (UIView *view in subviews)
{
    NSLog(@"class %@, tag: %i", [view class], view.tag);
} 

Landscape
2012-06-18 17:56:43.906 TS Data[6513:b903] class UIImageView, tag: 0
2012-06-18 17:56:43.907 TS Data[6513:b903] class UILabel, tag: 0
2012-06-18 17:56:43.908 TS Data[6513:b903] class UILabel, tag: 0
2012-06-18 17:56:43.908 TS Data[6513:b903] class UIThreePartButton, tag: 1
2012-06-18 17:56:43.909 TS Data[6513:b903] class UITextView, tag: 1000
2012-06-18 17:56:43.910 TS Data[6513:b903] class UIAlertTextView, tag: 12345
2012-06-18 17:56:43.910 TS Data[6513:b903] class UIImageView, tag: 3334

Portrait
2012-06-18 17:56:38.035 TS Data[6513:b903] class UIImageView, tag: 0
2012-06-18 17:56:38.053 TS Data[6513:b903] class UILabel, tag: 0
2012-06-18 17:56:38.054 TS Data[6513:b903] class UILabel, tag: 0
2012-06-18 17:56:38.055 TS Data[6513:b903] class UIThreePartButton, tag: 1
2012-06-18 17:56:38.055 TS Data[6513:b903] class UITextView, tag: 1000

Upvotes: 0

Views: 928

Answers (4)

Julius
Julius

Reputation: 11

You only have to change the 'message' property when you init the UIAlertView:

showSelection = [[UIAlertView alloc] 
             initWithTitle: @""
             message: @""
             delegate: nil 
             cancelButtonTitle: @"OK" 
             otherButtonTitles: nil];

Note that if you want to resize the UIAlertView you can use the following code in your view controller:

- (void) willPresentAlertView:(UIAlertView *)alertView
{
    alertView.frame = CGRectMake(0, 0, 150, 250);
}

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107121

You have 3 options to solve this:

  1. Manually set the frame according to the rotation. Like the answer given by "Warif Akhand Rishi"
  2. If the alertview is present then don't allow the view to rotate. (Return NO from the shouldAutoRotateToInterfaceOrientation method)
  3. Dismiss the alert view when interface is changing and display it after the view rotation. It needs a little bit amount of code. If there is anything in your textField then you want to temporarily store that and again display it.

I've faced the same issue in my project, at that time I choosed the second option.

Upvotes: 0

Warif Akhand Rishi
Warif Akhand Rishi

Reputation: 24248

On the iPhone Landscape mode alertView frame changes. I handled it manually by changing textField frame on orientation.

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
   if (((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone))
   {
        yourTextField.frame = CGRectMake(20.0, 32.0, 245.0, 25.0);
   }
   else
   {
        yourTextField.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
   }
}

Upvotes: 0

Ladislav
Ladislav

Reputation: 7283

Add:

myTextView.tag = 1000 //number here does not matter as long as it is not 0

then before you init your myTextView just check if view with tag 1000 exists with:

if (![showSelection viewWithTag:1000]){//if that view is not there add it, if not, it is there ;)
 UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(12.0, 15.0, 260.0, 210.0)];
 myTextView.tag = 1000
 myTextView.text = key;
 myTextView.dataDetectorTypes = UIDataDetectorTypeAll;//set the textview to pick up all link types
 [myTextView setFont:[UIFont fontWithName:@"ArialMT" size:16]];
 myTextView.textAlignment = UITextAlignmentCenter; 
 myTextView.editable = false;//the textview needs to be uneditable for the dataDectector to work
 myTextView.scrollEnabled = true;
 [myTextView setBackgroundColor:[UIColor whiteColor]];
 [showSelection addSubview:myTextView];
}

Upvotes: 0

Related Questions