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

5
  • what is this [alert setValue:VC.view forKey:@"accessoryView"];. UIAlertView does not provide that method Commented Mar 11, 2014 at 11:43
  • @Himanshu Joshi, this method is used in iOS7 to any subviews to alertView. See, for example, stackoverflow.com/a/21067447/2066428 Commented Mar 11, 2014 at 11:51
  • i saw it, but it didn't help me for this problem Commented Mar 11, 2014 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 developer.apple.com/library/ios/documentation/uikit/reference/… 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. Commented Mar 11, 2014 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.. Commented Nov 10, 2014 at 18:29

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

if i change my code to like this, it's not showing my ViewController's view
This code doesn't push the view controller. Neither did your original code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.