【问题标题】:Making a UIButton change a UIImageView image使 UIButton 更改 UIImageView 图像
【发布时间】:2013-04-24 19:13:57
【问题描述】:

我对编程和目标 c 非常陌生,所以请放轻松。

我想要一个 UIButton(我将其用作 IBAction)在按下时更改 UIImageView 中的图像。我将 UIImageView 放入情节提要中的 UIScrollView 中,但我希望我仍然可以以编程方式更改图像。

我已经在任何地方搜索了几个小时的答案,但没有什么对我有用,因为这不是正确的解决方案,或者我没有正确地做到这一点。

我已经尝试过这段代码以及其他一些代码,但是当我按下按钮时它们总是返回“线程 1:信号 SIGABRT”:

imageView.image = [UIImage imageNamed: @"Ruler pic inch.png"];

到目前为止,这是我的代码:

ViewController.h

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface ViewController : UIViewController <ADBannerViewDelegate, UIScrollViewDelegate>      {

ADBannerView *adView;
BOOL bannerIsVisible;
IBOutlet UIScrollView *scrollView;
IBOutlet UIImageView *imageView;
IBOutlet UIButton *proVersion;
IBOutlet UIButton *howToUse;

}

- (IBAction)switchUnit:(id)sender;

@property (nonatomic, assign) BOOL bannerIsVisible;
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize bannerIsVisible;
@synthesize scrollView;
@synthesize imageView;


- (void)viewDidLoad
{
[super viewDidLoad];

// Hide status bar:
[[UIApplication sharedApplication] setStatusBarHidden:YES];

// iAd:
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50.0f);
[self.view addSubview:adView];
adView.delegate=self;
self.bannerIsVisible=NO;

// Setting scrollview content size to size of image
scrollView.contentSize = CGSizeMake(320,2246);

}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {

    [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
    banner.frame = CGRectOffset(banner.frame, 0, 50.0f);
    [UIView commitAnimations];
    self.bannerIsVisible = YES;

    }
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {

    [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
    banner.frame = CGRectOffset(banner.frame, 0, -50.0f);
    [UIView commitAnimations];
    self.bannerIsVisible = NO;

    }
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (IBAction)switchUnit:(id)sender {

// CODE THAT MAKES IMAGE CHANGE GOES HERE (I thought...)

}

@end

我们将不胜感激。我受够了。我可能在某个地方犯了一个愚蠢的错误,但我无法解决。提前致谢。

【问题讨论】:

    标签: objective-c image uiimageview uibutton uiimage


    【解决方案1】:

    一般指针:

    1. 尝试将@property 用于您的所有实例变量(它将为您提供良好的服务)
    2. 不要使用@synthesize(编译器会更好地为您工作)
    3. 如果您有属性bob,请使用self.bob 访问它
    4. 开启 ARC(您目前似乎没有开启)
    5. 最好不要在图片名称中包含任何空格

    为了明确属性,当你有一个属性时,你不需要在@interface 的 {} 之间创建实例变量。这些应该被删除以防止多个定义。编译器完成的自动综合将为您定义实例变量,但您应该始终使用属性来访问值(如上面的第 3 项)。

    也就是说,您的 - (IBAction)switchUnit:(id)sender 看起来不错,如果有点空的话。代码应该是这样的:

    self.imageView.image = [UIImage imageNamed:@"Ruler_pic_inch.png"];
    

    要让它工作,图像Ruler_pic_inch.png 需要在你的包中(这意味着它在 Xcode 项目中并设置为在构建期间复制。

    再次,也就是说,您的问题似乎与您在某个地方感到困惑有关。 SIGABRT 类型问题通常会被编译器拾取并作为警告通知您,例如“某些对象可能无法响应某些选择器”。如果您看到其中任何一个,请查看它们并弄清楚您为什么要尝试向对象提出它不理解的问题。

    如果您没有看到任何编译器警告,那么您已经告诉编译器一个对象将属于一种类型,然后实际上将其设置为其他类型。在这种情况下,我会在 Storyboard 中查看您的 IBOutlets 并检查它们是否实际设置为正确的目标视图(断开并重新连接它们以确保它们)。

    【讨论】:

    • 非常感谢!我仔细阅读了您的答案,并通过执行您重新连接 IBOutlets 的最后建议来设法使其正常工作。我喜欢这个网站。但是关于“图像名称中没有空格”。我按照您的建议做了,但它只是给出了一个黑色图像,但是在用空格替换下划线后它可以正常工作。感谢您解释 SIGABRT 错误,因为我之前遇到过其中的几个并且让我感到困惑。
    • 一个问题,不合成属性时是否存在“语义问题”?谢谢
    • 这取决于 Xcode 所说的“语义问题”是什么。一般来说,任何警告都是应该解决的问题。
    • 这就是语义问题所说的:自动合成的属性“bannerIsVisible”将使用合成的实例变量“_bannerIsVisible”,而不是现有的实例变量“bannerIsVisible”,它对我没有合成的所有内容都这么说。
    • 我在答案中添加了更多内容。基本上,删除您拥有属性的实例变量并确保您使用的是“自我”。访问它们。
    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多