[英]How to add subview to another viewcontroller?
I'm doing an Ipad app. 我正在做一个Ipad应用程序。 Now I have 2 viewcontrollers, ViewController has a button1 which has a popover segue to the second viewcontroller(PopoverController).
现在我有2个viewcontrollers,ViewController有一个button1,它有一个popover segue到第二个viewcontroller(PopoverController)。 Then, the PopoverController has a button2, if I click the button2, I'll receive some UIImage from my server.
然后,PopoverController有一个button2,如果我点击button2,我将从我的服务器收到一些UIImage。 I want to add fews subviews of UIImageView to the ViewController to display these images if I click the button2.
我想在ViewController中添加几个UIImageView的子视图,以便在我单击button2时显示这些图像。
The button1 works well, the PopoverController can pop up as expected. button1运行良好,PopoverController可以按预期弹出。 BUT when I click the button2, nothing happend.
但是,当我点击按钮2时,没有任何事情发生。 I want to know how can I pass the data between 2 viewcontrollers and how to add subviews to another one.
我想知道如何在两个视图控制器之间传递数据以及如何将子视图添加到另一个视图控制器。
Some codes relating to my problem: 一些与我的问题有关的代码:
ViewController.h: ViewController.h:
#import <UIKit/UIKit.h>
@class PopoverController;
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *button1;
@property (strong, nonatomic) PopoverController *popoverController;
@end
PopoverController.h: PopoverController.h:
#import <UIKit/UIKit.h>
@class ViewController;
@interface PopoverController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *button2;
@property (strong, nonatomic) UIImage *tempImg;
@property (strong, nonatomic) ViewController *viewController;
- (IBAction)addsubviews:(id)sender;
@end
I can not just use [viewController.view addSubview:img1];
我不能只使用
[viewController.view addSubview:img1];
in the - (IBAction)addsubviews:(id)sender;
在
- (IBAction)addsubviews:(id)sender;
method to addsubview. addsubview的方法。 So someone can help me?
有人可以帮助我吗? :)
:)
====1st update==== ====第一次更新====
Someone suggest that I have to use - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
method. 有人建议我必须使用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
方法。 I have tried this one by control click the button2 and create a custom segue between button2 and ViewController. 我通过控件尝试了这个,单击button2并在button2和ViewController之间创建自定义segue。 When I clicked the button2, it showed :
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'change'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
当我点击button2时,它显示:
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'change'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'change'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
So I'm wondering whether I should add a NavigationController
. 所以我想知道是否应该添加一个
NavigationController
。 If so, what should I do? 如果是这样,我该怎么办?
====2nd update==== ====第二次更新====
I use Paramasivan 's code now, and I found the way to call method from another viewcontroller. 我现在使用Paramasivan的代码,我找到了从另一个viewcontroller调用方法的方法。 The problem now is the newly added subview in my viewcontroller doesn't show up.
现在的问题是我的viewcontroller中新添加的子视图没有显示出来。 I guess I have to update my viewcontroller in order to make it visible.
我想我必须更新我的viewcontroller以使其可见。
in my - (IBAction)addsubviews:(id)sender;
在我的
- (IBAction)addsubviews:(id)sender;
method, i invoke the method in ViewController by [self.viewController createSubViewWithImage:_tempImg];
方法,我通过
[self.viewController createSubViewWithImage:_tempImg];
调用ViewController中的方法[self.viewController createSubViewWithImage:_tempImg];
so the method can be invoked when i click the button2, but the view of viewcontroller has nothing changed. 所以当我点击button2时可以调用该方法,但viewcontroller的视图没有任何改变。
查看“ 与对象通信”文档,有几种方法可以执行您想要的操作。
In ViewController.h add the following 在ViewController.h中添加以下内容
-(void)createSubViewWithImage:(UIImage *)imageDownloaded {
UIImageView *imageViewTemp = [[UIImageView alloc] initWithImage:imageDownloaded];
[self.view addSubView:imageViewTemp];
}
In PopoverController.h add the following 在PopoverController.h中添加以下内容
@property (nonatomic, retain) ViewController *viewControllerPassed;
And after image downloaded, call the following in PopoverController.h 下载映像后,在PopoverController.h中调用以下内容
[viewControllerPassed createSubViewWithImage:imageDownloaded];
Add this in - (void)viewDidLoad
, 将此添加到
- (void)viewDidLoad
,
self.popoverController = [[PopoverController alloc] init];
self.popoverController.viewController = self;
Make sure that in no other places, you are setting self.popoverController = ...
. 确保在其他任何地方都没有设置
self.popoverController = ...
Do NOT add anything like self.viewController = ...
in popovercontroller
class. 不要添加其它东西一样
self.viewController = ...
在popovercontroller
类。 And you dont have to do self.viewController.popoverController = self;
你不必做
self.viewController.popoverController = self;
as well. 同样。 Just remove these lines if you already have it.
如果您已经拥有它们,请删除它们。
Once these are done, make sure that you are displaying self.popoverController
only in the popover and you are not creating a new object for popoverController
class there. 完成这些操作后,请确保仅在弹出窗口中显示
self.popoverController
,而不是在那里为popoverController
类创建新对象。 So if these are fine, you can use any approach you want for passing the image from popoverController
class to viewController
class. 因此,如果这些都很好,您可以使用任何方法将图像从
popoverController
类传递给viewController
类。
as you mentioned in your comment you can use [self.viewController createSubViewWithImage:_tempImg];
正如您在评论中提到的,您可以使用
[self.viewController createSubViewWithImage:_tempImg];
in your popovercontroller
class. 在你的
popovercontroller
类中。
Update: If you are doing via storyboard, you need to set this in prepareForSegue
method and you dont have to create self.popoverController
at all. 更新:如果你是通过故事板进行的,你需要在
prepareForSegue
方法中设置它,你self.popoverController
创建self.popoverController
。 Remove that part in your case. 在你的情况下删除该部分。 You can follow the procedure mentioned here to set up a custom segue and implement
prepareForSegue
method to pass the object. 您可以按照此处提到的过程设置自定义segue并实现
prepareForSegue
方法来传递对象。 Source: On storyboards, views and passing data along 来源: 在故事板,视图和传递数据
prepareForSegue
method prepareForSegue
方法 viewController
in the popoverController
object there as, viewController
在popoverController
对象那里, Try, 尝试,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"CustomSegue"]) {
PopoverController *popoverController = [segue destinationViewController];
popoverController.viewController = self;
}
}
After doing this, you need to call [self.viewController createSubViewWithImage:_tempImg];
执行此操作后,您需要调用
[self.viewController createSubViewWithImage:_tempImg];
in your popoverController
class. 在你的
popoverController
类中。
You can pass data using the below method: 您可以使用以下方法传递数据:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ViewController2"])
{
ViewController2 *v2VC = [segue destinationViewController];
v2VC.yourData = self.someStuff;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.