这就是我要做的:iOS的显示图像点击
在点击“信息(I)”按钮,我需要显示有一个酥料饼左边的图像和右边的一些相关文字。
现在,让我们在后台将视图称为“视图1”,将弹出视图视为“视图2”。
这里是View2Controller.h的样子:
@class View2Controller;
@protocol View2ControllerDelegate <NSObject>
- (void)infoPopover:(View2Controller *)obic loadImage: (NSString *)imageFilename;
@end
@interface View2Controller : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageInfo;
@property (strong, nonatomic) IBOutlet UILabel *textInfo;
@property (nonatomic, assign) id<View2ControllerDelegate> delegate;
@end
id <View2ControllerDelegate> _delegate;
现在View1Controller.h有这样一行:
@interface View1Controller : UIViewController <View2ControllerDelegate>
而且View1Controller.m的@implementation颇像这
- (IBAction)showInfo:(id)sender {
UIButton *btn = (UIButton *)sender;
View2Controller *obic = [[View2Controller alloc]init];
[obic setDelegate:self];
switch (btn.tag) {
case 1:
[self infoPopover:obic loadImage:@"info1.png"];
break;
case 2:
//Info View 2
break;
case 3:
//Info View 3
break;
case 4:
//Info View 4
break;
case 5:
//Info View 5
break;
case 6:
//Info View 6
break;
default:
break;
}
}
and
- (void)infoPopover:(View2Controller *)obic loadImage:(NSString *)imageFilename
{
[obic.imageInfo setImage:[UIImage imageNamed:imageFilename]];
}
我查阅了几个关于代表如何工作的教程。但是我仍然无法在'UIImageView imageinfo'上显示图像'info1.png'。任何意见都非常感谢!
你的问题可能是你如何用alloc init创建obic。这很可能会创建View2Controller的新实例,而不是访问已有的实例。你需要解释你的2个控制器是如何创建的,以及你的控制器的总体层次结构是什么(它们是否在导航控制器,分割视图等中)。 – rdelmar
一旦单击'i'按钮,我就会使用'popover'从View1Controller切换到View2Controller。 我刚刚意识到View1Controller(以及一些其他控制器)嵌入在导航控制器中,但View2Controller不是。应该将View2Controller嵌入到导航控制器中吗? – hrshd