0

I have ViewController with UITableView. if i touch on any Cell, it shows information about this Cell in another view. It is working well. But when i add MyViewController's view to UIAlerView's view, if i touch on AlertView, it is giving runtime error. this is my code:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
MyViewController *VC = [[MyViewController alloc] init];
[alert setValue:VC.view forKey:@"accessoryView"];
[alert show];

any help please ... my program shows like this:

enter image description here

  • what is this `[alert setValue:VC.view forKey:@"accessoryView"];`. `UIAlertView` does not provide that method – Himanshu Joshi Mar 11 '14 at 11:43
  • @Himanshu Joshi, this method is used in iOS7 to any subviews to alertView. See, for example, http://stackoverflow.com/a/21067447/2066428 – malex Mar 11 '14 at 11:51
  • i saw it, but it didn't help me for this problem – Ayubxon Ubaydullayev Mar 11 '14 at 12:01
  • 1
    Just wondering if you know the rule about not subclassing or changing the view hierarchy of a `UIAlertView`??? Please the section marked as `"Subclassing note"` for this details https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html So you shouldn't really be messing with this. If Apple wanted you or allowed you to play with this they would provide a property for such things a long the lines of `setAccessoryView:`. This will most likely get your app rejected. – Popeye Mar 11 '14 at 12:05
  • how did you set accessoryView frame so big?? I set accessoryView value to a UIView but it doesn't show that big, it gets cut.. – Frade Nov 10 '14 at 18:29

1 Answers1

3

An alert view is simply shown on the screen. It doesn't matter which view controller is currently visible, when the show method is called, the alert view is added as the top view.

The only thing that would matter in terms of view controllers is which one you want as the delegate view controller, which can be assigned to objects other than self.

Refactor your code to look like this:

MyViewController *VC = [[MyViewController alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"" delegate:VC cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];

Notice how the VC you want to display is now the delegate to the alert?

nhgrif
  • 61,578
  • 25
  • 134
  • 173