【问题标题】:How to display images on button action如何在按钮操作上显示图像
【发布时间】:2014-03-06 08:30:36
【问题描述】:

我有 10 个UIImages。当我单击UIButton 它显示在UIScrollView. But I need to implement theUIButtonlike **Next**, ifUIButtonis clicked first time, it displays the firstUIImage` 下,然后单击它会显示下一个图像。如果我点击后退按钮,应该会显示上一张图片。

int   currentIndex = 0;
int MAX_COUNT;

NSMutableArray *imageName = [[NSMutableArray alloc] initWithObjects:
                                @"spices.jpg",
                                @"spice_powder.jpg", 
                                @"turmeric.jpg",
                                @"whynani_img3.jpg",
                                @"spice_blends.jpg",
                                @"products1.png", nil];

currentIndex = currentIndex + 1;
if(currentIndex > MAX_COUNT) {
   currentIndex = MAX_COUNT;
}        

for (int i = 0; i<[imageName count]; i++ ) {

   UIImageView  *mmageView = 
     [[UIImageView alloc] initWithFrame:CGRectMake(200,200,350,350)];
   [mmageView setImage:[UIImage imageNamed:[imageName objectAtIndex:i]]];            
   [self.view addSubview:mmageView];           
}

【问题讨论】:

  • 只需根据其索引制作图像数组并点击显示即可。
  • @HimanshuJoshi:你能帮我编辑一下代码吗?

标签: ios objective-c uiscrollview uibutton uiimage


【解决方案1】:

在接口文件中声明一个全局imageView和一个int

UIImageView *imgView;
int index;

viewDidLoad:

index = 0;

imgView = [[UIImageView alloc] initWithFrame:CGRectMake(200,200,350,350)];
[imgView setImage:[UIImage imageNamed:[imageName objectAtIndex:index]]];
[self.view addSubView:imgView]; 

Next Button Action

-(void)nextButtonAction
{
  ++index;

  [previousBtn setEnabled:YES]; 

  if (index > [imageName count])
  {
    index = [imageName count] - 1;
    [nextBtn setEnabled:NO];
  }

  [imgView setImage:[UIImage imageNamed:[imageName objectAtIndex:index]]];

}

Previous Button Action

-(void)previousButtonAction
{
  --index;

  [nextBtn setEnabled:YES]; 

  if (index < 0)
  {
    index = 0;
    [previousBtn setEnabled:NO];
  }

  [imgView setImage:[UIImage imageNamed:[imageName objectAtIndex:index]]];

}

【讨论】:

    【解决方案2】:

    您可以使用具有 2 个 UIButtons 的 UIViewController 来执行相同的操作,其中自定义图像与 UIImageView 一起描绘前进和后退。在选择下一个/后退按钮时加载适当的图像。图像名称必须存储在 NSArray 中。

    希望这会有所帮助。

    编辑:示例代码如下,

    在.h文件中

    @property(nonatomic, weak) UIImageView *imageView;
    @property(nonatomic, weak) UIButton *nextButton;
    @property(nonatomic, weak) UIButton *backButton;
    
    (IBAction)btnNextBtn:(id)sender;
    (IBAction)btnBackBtn:(id)sender;
    

    在 .m 文件中

    (void)viewDidLoad {
        currentIndex = 0;
        NSMutableArray *imageName = [[NSMutableArray alloc] initWithObjects:@"Myprofile.png", 
                                     @"great_ideas_wordle2.png", @"Review.png", 
                                     @"collaborate.png", nil];
    
       imageView = [[UIImageView alloc] initWithFrame:CGRectMake(200,200,350,350)];
       [imageView setImage:[UIImage imageNamed:[imageName objectAtIndex:currentIndex]]];
       [self.view addSubView:imageView];
    }
    
    (IBAction)btnNextBtn:(id)sender {
         currentIndex = currentIndex + 1;
         if(currentIndex > MAX_COUNT) {
             currentIndex = MAX_COUNT;
         }
         // load the image view with the currentIndex from the array.
         [imageView setImage:[UIImage imageNamed:[imageName objectAtIndex:currentIndex]]];
    }
    
    (IBAction)btnBackBtn:(id)sender {
         currentIndex = currentIndex - 1;
         if (currentIndex < 0) {
             currentIndex = 0;
         }
         // load the image view with the currentIndex from the array.
         [imageView setImage:[UIImage imageNamed:[imageName objectAtIndex:currentIndex]]];
    }
    

    【讨论】:

    • 在您的 for 循环中,您正在创建新的图像视图并将其添加到 UIView 中,这是不正确的。您使用情节提要创建 UIImageView 和 UIButton。应该使用相同的 UIImageView 来显示图像。
    • 我不明白你的逻辑。我正在创建 UIImageView 和按钮。我没有使用故事板。不将 UIImageView 添加到 UIView 那么它将如何显示?
    • 不正确。如果您使用的是 XIB,则添加这些 UIComponents 并将您的组件链接到其各自的变量名称。
    • 这里我的代码有什么错误。我能做些什么。再次告诉我没有使用故事板。我应该怎么称呼 herr // 使用数组中的 currentIndex 加载图像视图。
    • 您是在使用 XIB 还是以编程方式创建 UIControls?请找到我编辑的答案。
    【解决方案3】:

    将其写入 .h 文件

    @property (nonatomic,retain)IBOutlet UIImageView *imageview1;
    
    @property (nonatomic,retain)IBOutlet UIButton *pre,*next;
    
    @property (nonatomic,retain) NSArray *images;
    
    @property (nonatomic) int currentindex;
    
    -(IBAction)prebtn:(id)sender;
    
    -(IBAction)nextbtn:(id)sender;
    

    将此代码写入 .m 文件

    @implementation ViewController
    
    @synthesize images;
    @synthesize currentindex;
    @synthesize pre,next;
    @synthesize imageview1;
    
    - (void)viewDidLoad
    {
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
    
      self.currentindex = 0;
    
      self.images = [[NSArray alloc] initWithObjects:@"images-2.jpeg",@"images-    3.jpeg",@"images.jpeg", nil];
     imageview1.image = [UIImage imageNamed:@"images-2.jpeg"];
     [pre setEnabled:NO];
      } 
    
    - (void)didReceiveMemoryWarning
      {
     [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    
    -(IBAction)nextbtn:(id)sender{
    currentindex = currentindex + 1;
    [pre setEnabled:YES];
    if (currentindex >= [images count])
    {
    currentindex = currentindex - 1;
    [next setEnabled:NO];
    }
    imageview1.image = [UIImage imageNamed:[images objectAtIndex:currentindex]];    
    }
    
    -(IBAction)prebtn:(id)sender{
    
    currentindex = currentindex - 1;
    [next setEnabled:YES];
    if(currentindex < 0)
    {
    currentindex = 0;
    [pre setEnabled:NO];
    }
    imageview1.image = [UIImage imageNamed:[images objectAtIndex:currentindex]];
    }
    
    @end
    

    【讨论】:

      【解决方案4】:

      试试这个代码

      .h 文件

      @property(nonatomic, strong) UIImageView *imageView;
      @property(nonatomic, strong) UIButton *nextButton;
      @property(nonatomic, strong) UIButton *previousButton;
      @property(nonatomic, strong) NSMutableArray *imagesArrayName;
      @property(nonatomic) int   currentIndex;
      
      -(IBAction)btnNextImage:(id)sender;
      -(IBAction)btnPreviousImage:(id)sender;
      

      .m 文件

      - (void)viewDidLoad {
          [super viewDidLoad];
      
          self.currentIndex = 0;
      
          NSMutableArray *imagesArrayName = [[NSMutableArray alloc] initWithObjects:
                                            @"spices.jpg",
                                            @"spice_powder.jpg", 
                                            @"turmeric.jpg",
                                            @"whynani_img3.jpg",
                                            @"spice_blends.jpg",
                                            @"products1.png", nil];
      
          [self.nextButton addTarget:self action:@selector(btnNextImage:) forControlEvents:UIControlEventTouchUpInside];
          [self.previousButton addTarget:self action:@selector(btnPreviousImage:) forControlEvents:UIControlEventTouchUpInside];
      
          [self.imageView setImage[UIImage imageNamed:(NSString*)[imagesArrayName objectAtIndex:currenIndex]]];
      
          [self.previousButton setEnabled:FALSE];
      
      }
      
      -(IBAction)btnNextImage:(id)sender {
      
          self.currentIndex = self.currentIndex + 1;
      
          [self.imageView setImage[UIImage imageNamed:(NSString*)[imagesArrayName objectAtIndex:currenIndex]]];
      
          if (self.currentIndex == [self.imagesArrayName count] - 1) {
      
              [self.nextButton setEnabled:FALSE];
          }
          [self.previousButton setEnabled:TRUE];
      
      }
      -(IBAction)btnPreviousImage:(id)sender{
      
          self.currentIndex = self.currentIndex - 1;
      
          [self.imageView setImage[UIImage imageNamed:(NSString*)[imagesArrayName objectAtIndex:currenIndex]]];
      
          if (self.currentIndex == 0) {
      
              [self.previousButton setEnabled:FALSE];
          }
          [self.nextButton setEnabled:TRUE];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-23
        • 1970-01-01
        相关资源
        最近更新 更多