Objective-c: presentViewController from UIView

2019-09-03 18:03发布

Im trying to use presentViewController inside a UIView.

http://img145.imageshack.us/img145/6334/appt.png

When I click he "+" button at the top/right, I want to replacer the tableview at the right (in the grey part of the image) by a UIViewController.

- (void)add:(id)sender
{
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"testview"];
[self presentViewController:controller animated:YES completion:NULL];
}

Of course by doing this, the whole screen is replaced.. I want something like this:

[contentView addSubview:controller];

or

[contentView presentViewController:controller animated:YES completion:NULL];

where contentView is an UIView containing the tableview

I found this addChildViewController and presentViewController but I don't think that can help me out

Thanks

EDIT:

Use ContainerView in storyboard solved my problem

5条回答
混吃等死
2楼-- · 2019-09-03 18:13

A PopoverController seems to be what you need. Here is some boilerplate code:

UIPopoverController* aPopover = [[UIPopoverController alloc]
                                     initWithContentViewController:navigation];

self.aPopoverController = aPopover;


[self.aPopoverController presentPopoverFromRect:nil inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

aPopoverController is a property of the class.

@property UIPopoverController* aPopoverController;
查看更多
Explosion°爆炸
3楼-- · 2019-09-03 18:24

If I understand clearly. This would help you out

SomeViewController *viewController=[[SomeViewController alloc]init];

[viewController setFrame:CGRectMake(x,y,width,height)];

[self.view addsubview:viewController.view];

Sorry if Iam wrong...

查看更多
仙女界的扛把子
4楼-- · 2019-09-03 18:25

I am not sure if I understand you right. From your picture I believe you are programming for an iPad, and you want to cover the whole view partly by another view. If so, you could use a UIPopoverController, which does exactly this.

查看更多
贼婆χ
5楼-- · 2019-09-03 18:29

You can add the controller's view as a subview of your contentView. Be sure to keep a strong reference to the controller when you do that though.

Another approach would be to make the new view owned by the presenting controller instead of a second controller and simply hide/show views as needed.

查看更多
Melony?
6楼-- · 2019-09-03 18:30

edit your statement:

[self.navigationController presentViewController:controller animated:YES completion:NULL];

try this and then let me know if it worked!

查看更多
登录 后发表回答