1

I have:

  • an UIViewController A
  • UIView B: I added subview which is a UIView to the UIViewController A
  • an UIViewController C

What I did is:

in UIViewController A's viewDidLoad's method, I call this:

UIView *subviewB = [[Subview alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[subviewB subviewBMethod]; // 
[self.view addSubview:subviewB];

SubviewBMethod code is to create an view, inside this view have a button. Once this button is clicked, it will change to UIViewController C.

I tried this:

[self presentModalViewController:self.UIViewControllerC animated:YES];

And I got this error message:

warning: incompatible Objective-C types 'struct UIViewControllerC *', expected 'struct UIViewController *' when passing argument 1 of 'presentModalViewController:animated:' from distinct Objective-C type

When I run the app, it terminates immediately when I click the button, I opened the console bug, there is no any error message. My method must be wrong, thus my question is:

Is it possible to load a UIVIewcontroller from UIView of another UIViewController? If:

  • Yes: How to do it?
  • No: What should I do?
Justin Boo
  • 10,132
  • 8
  • 50
  • 71
chako89
  • 11
  • 3

3 Answers3

1

It looks like you have a declaration like this:

 UIViewControllerC *UIViewControllerC;
                 ^ 

When you want:

 UIViewController *UIViewControllerC;
Hack Saw
  • 2,741
  • 1
  • 18
  • 33
  • I think UIViewControllerC really is what he wants, since it is his custom subclass. Declaring it that way will make him have to cast it every time he calls a custom method on his view controller. – Altealice Jan 13 '11 at 05:58
  • Oh, of course... But isn't it odd that you'd need to coerce it? If it's an subclass of UIViewController, the system would note that, and accept it, wouldn't it? – Hack Saw Jan 13 '11 at 06:06
  • Hi, I have both declared UIViewController A and C as @property(nonatomic, retain) type, syntesize and release it, but it still have the same warning message. Is it because UIView cant call this function: [self presentModalViewController:self.UIViewControllerC animated:YES];?? – chako89 Jan 13 '11 at 07:17
1

I've recently done the same thing. - I have a viewControllerA that has a seeMoreView (a custom view) on it. When the user clicks "see more", viewControllerB is presented.

I solved it like this:

  1. I subclassed seeMoreView.

  2. I declared a delegate in the seeMoreView - syntax in this answer: Delegates Vs. Notifications in iPhoneOS

  3. In viewControllerA I set the delegate of the seeMoreView as self when creating the view.

  4. In viewControllerA I implement the delegate method, which presents viewControllerB.

(I don't think it's good practice in the MVC paradigm to call a view controller from a view.)

Community
  • 1
  • 1
Caroline
  • 4,875
  • 2
  • 31
  • 47
0

Can you try this?

[self presentModalViewController:(UIViewController *)self.UIViewControllerC animated:YES];
Altealice
  • 3,572
  • 3
  • 21
  • 41
  • What? Are you sure self isn't pointing to a UIViewController? Because that should give a different error. Can you post your exact code so we can check? – Altealice Jan 13 '11 at 11:12