iOS 根据单击的 UIButton 在弹出窗口上显示图像

标签 ios objective-c delegates uiimageview popover

这就是我正在尝试做的事情:

screenshot

单击“信息 (i)”按钮时,我需要显示一个弹出窗口,左侧有图像,右侧有一些关联的文本。

现在,我们将后台 View 称为“View 1”,将弹出窗口称为“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]];
}

我查阅了一些有关委托(delegate)如何工作的教程。但我仍然无法在“UIImageView imageinfo”上显示图像“info1.png”。非常感谢任何评论!

最佳答案

如果你有一个 pop over 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 的委托(delegate),因此您可能误解了委托(delegate)的用途。假设 View2Controller 中有一个按钮,当它被按下时你想通知 View1Controller - 在 View2Controller 中你可以调用 [self.delegate theButtonWasPressed]; 并且无论委托(delegate)是什么(可以是一个实现了 View2ControllerDelegate 协议(protocol)的 View1Controller 实例)

关于iOS 根据单击的 UIButton 在弹出窗口上显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22849649/

相关文章:

ios - 从 NSDateFormatterLongStyle 中删除格式化字符串的一部分

java - 取消委托(delegate)方法

c# - 对 DataGridView 的非阻塞更新

ios - 检查 NSDictionary 中的特定值

ios - 将 CMTimeRange 拆分为多个 CMTimeRange block

ios - Firebase 可以在 iOS 7 后台发送和接收吗?

ios - 如果应用程序处于终止/强制关闭(不在后台)状态,收到推送通知时将调用什么委托(delegate)方法?

objective-c - 如何使用单独的sheetController显示不同NIB包含的工作表

c# - 如何知道 UserControl 何时完成触发事件?

ios - 已经在过渡时试图关闭演示 Controller