【问题标题】:Xcode Basic History FunctionXcode 基本历史功能
【发布时间】:2012-05-14 13:37:05
【问题描述】:

您好,我是 iPhone Xcode 开发的新手,一直坚持实现历史功能。

用外行的话我有

4 个按钮(每个按钮都有自己独特的图像)

(按钮 1) (按钮 2) (按钮 3) (按钮 4)

和 5 个图像视图

(图像视图位置 1) (图像视图位置 2) (图像视图位置 3) (图像视图位置 4) (图片浏览位置 5)

每次单击按钮(最多 5 次)时,我都需要将其显示在适当的图像视图中。

即如果首先按下按钮 4,它将显示在图像视图 1 中 如果第二次按下按钮 3,它将显示在图像视图 2 等处。

我非常感谢一些帮助或朝着正确的方向前进。

提前致谢


更新代码:

viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {


IBOutlet UIImageView *imageview_history_1;
IBOutlet UIImageView *imageview_history_2;
IBOutlet UIImageView *imageview_history_3;
IBOutlet UIImageView *imageview_history_4;
IBOutlet UIImageView *imageview_history_5;

}

// I would think this could be consolidated into one function
-(IBAction)showhistory_1;
-(IBAction)showhistory_2;
-(IBAction)showhistory_3;
-(IBAction)showhistory_4;
-(IBAction)showhistory_5; 

@end



#import ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



// function to add to history
// Need an if/for statement???
// On button event 1 display result at image view_history 1, 
// On button event 2 display result at image view_history 2.

- (IBAction)showhistory_1 {
UIImage *img = [UIImage imageNamed:@"button1"];

[imageview_history_1 setImage:img];

}


@end

//我一直在创建函数。再次感谢。

【问题讨论】:

  • 一般来说,为这类问题包含一些代码是个好主意,这样我们就知道你在哪里以及我们需要带你去哪里。
  • 嗨@RyanPoolos 希望更新后的代码有助于理解我想要实现的目标。再次感谢。
  • 并且不要使用这样的帖子标题,因为该标题不适合这个问题。

标签: iphone xcode ios4 uiimageview xcode4.2


【解决方案1】:

首先,在你的 xib 中,给每个按钮一个标签号以便于识别它:
假设你给 button1 一个标签 1,button2 是标签 2,等等。

然后我会将您的 UIButton 操作更改为都指向同一个函数:

.h:

- (IBAction)show_history:(UIButton)sender;

.m:

- (void)setNextHistoryImage:(UIImage *)img {
    if (imageview_history_1.image == nil) {
        imageview_history_1.image = img;
    } else if (imageview_history_2.image == nil) {
        imageview_history_2.image = img;
    }  else if (imageview_history_3.image == nil) {
        imageview_history_3.image = img;
    }  else if (imageview_history_4.image == nil) {
        imageview_history_4.image = img;
    }  else if (imageview_history_5.image == nil) {
        imageview_history_5.image = img;
    }
}

- (IBAction)show_history:(UIButton)sender {
    NSString *imgName = [NSString stringWithFormat:@"button%d", sender.tag];
    UIImage *img      = [UIImage imageNamed:imgName];
    [self setNextHistoryImage:img];
}

【讨论】:

  • 我收到以下错误 Incompatible pointer types assignmenting to 'UIImageView *__strong' from 'UIImage *__strong'
  • 糟糕,抱歉。更正了代码以引用图像视图的图像属性。这应该会照顾它。
  • 为迟到的回复道歉,是的。感谢你的帮助。我的游戏已提交到应用商店。我将在此处发布一个链接。再次感谢
【解决方案2】:

你可以这样做:

-(IBAction)showhistory_1:(id)sender
{

    if(sender == firstbutton)
    {
       UIImage *img = [UIImage imageNamed:@"button4"];
       [imageview_history_4 setImage:img];
    }
    else if(sender == secondbutton)
    {
       UIImage *img = [UIImage imageNamed:@"button3"];
       [imageview_history_3 setImage:img];
    }
    else if(sender == thirdbutton)
    {
      UIImage *img = [UIImage imageNamed:@"button2"];
      [imageview_history_2 setImage:img];
   }
   else if(sender == fourthbutton)
   {
      UIImage *img = [UIImage imageNamed:@"button1"];
      [imageview_history_1 setImage:img];
   }
}

【讨论】:

    【解决方案3】:

    做一件事只是改变按钮点击时imageview的位置。像这样使用,如果它的工作告诉我们,如果没有,我也会提供更多的方法。

      ImageView.frame=CGRectMake(0, 0, 305, 135);
    

    使用不同的 2 位置。

    【讨论】:

      猜你喜欢
      • 2010-11-06
      • 2013-11-21
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多