f00pman
f00pman

Reputation: 51

how to add a UITableView into UIAlertView in iOS 7

did some googling around saw that subview is no longer supported in iOS 7.

Some ppl recommend creating custom view, but i am not sure how can i do that.

Here is my code, can anyone point me in the correct direction?

-(IBAction)click_select_fruit_type
{
select_dialog = [[[UIAlertView alloc] init] retain];
[select_dialog setDelegate:self];
[select_dialog setTitle:@"Fruit Type"];
[select_dialog setMessage:@"\n\n\n\n"];
[select_dialog addButtonWithTitle:@"Cancel"];

idType_table = [[UITableView alloc]initWithFrame:CGRectMake(20, 45, 245, 90)];
idType_table.delegate = self;
idType_table.dataSource = self;
[select_dialog addSubview:idType_table];

[idType_table reloadData];

[select_dialog show];
[select_dialog release];

}

Upvotes: 3

Views: 9963

Answers (3)

malex
malex

Reputation: 10096

You can change accessoryView to any own customContentView in a standard alert view in iOS7

[alertView setValue:customContentView forKey:@"accessoryView"];

Note that you must call this before [alertView show].

Simplest illustrating example:

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
v.backgroundColor = [UIColor yellowColor];
[av setValue:v forKey:@"accessoryView"];
[av show];

enter image description here

Real tableView as the subview of UIAlertView example:

enter image description here

Upvotes: 13

Vinzzz
Vinzzz

Reputation: 11724

you'll have to subclass UIViewController and use its preferredContentSize property to do some 'custom modal' mimicking UIAlertView layout

Upvotes: 0

Enrico Susatyo
Enrico Susatyo

Reputation: 19790

You can't. Apple deprecated ability to add any subviews to UIAlertView in iOS 7. Which I think is a good decision. A lot of people abused UIAlertView.

Creating a custom view is a good idea, but that's not what you wrote in your code. It seems like you are adding subviews to UIAlertView again.

See Alert View UI guide here.

Upvotes: 1

Related Questions