根据单击的UIButton,iOS在popover上显示图像

时间:2014-04-03 21:48:49

标签: ios objective-c delegates uiimageview popover

这就是我想要做的事情:

screenshot

点击'info(i)'按钮后,我需要显示一个左侧有图像的弹出窗口,右侧有一些相关文字。

现在让我们在后台调用视图为“View 1”,将popover调用为“View 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;
}
}

- (void)infoPopover:(View2Controller *)obic loadImage:(NSString *)imageFilename
{
    [obic.imageInfo setImage:[UIImage imageNamed:imageFilename]];
}

我查了几个关于代表如何工作的教程。但我仍然无法在'UIImageView imageinfo'上显示图像'info1.png'。非常感谢任何评论!

2 个答案:

答案 0 :(得分:1)

如果你有一个链接到你的信息按钮的segue,那么你不需要创建View2Controller - segue会为你做这个。

你可以摆脱showInfo IBAction方法并添加prepareForSegue:方法 -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    View2Controller *obic=(View2Controller)segue.destinationViewController;
    obic.delegate=self;

    UIButton *btn = (UIButton *)sender;


    NSString *imageFilename=nil;

    switch (btn.tag) {
    case 1:
        imageFilename=@"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;
   }

   if (image!=nil)
   { 
       [obic.imageInfo setImage:[UIImage imageNamed:imageFilename]];
   }
   else
   {
       NSLog(@"Unknown button tag triggered segue - %d",btn.tag);
   }
}

此处您没有显示任何内容需要View1Controller充当View2Controller的委托,因此您可能误解了使用的委托。假设View2Controller中有一个按钮,你想在按下时通知View1Controller - 在View2Controller中你可以调用[self.delegate theButtonWasPressed];,这会调用该方法,无论委托是什么(可能是实现View2ControllerDelegate的View1Controller实例)协议)

答案 1 :(得分:0)

解决!!

正如Paulw11之前所建议的那样,代表不是首先要走的路。所以我把代理代码拿出来了。我还拿出了IBAction showinfo。相反,我使用了方法:

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
   if ([identifier isEqualToString:@"info"]) {
        UIButton *btn = (UIButton *)sender;
    [[NSUserDefaults standardUserDefaults]setInteger:btn.tag forKey:@"infoButton"];
}
return YES;

在View1Controller和View2Controller中我添加了:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *imageArray = @[@"info1.png", @"info2.png", @"info3.png", @"info4.png", @"info5.png", @"info6.png"];
    int infoButtonChoice = [[NSUserDefaults standardUserDefaults] integerForKey:@"infoButton"];

    [self.imageInfo setImage:[UIImage imageNamed:imageArray[infoButtonChoice -1]]];
}

这使得应用程序完全按照我想要的方式工作!!