【问题标题】:How to add next and previous buttons programmatically in iPhone?如何在 iPhone 中以编程方式添加下一个和上一个按钮?
【发布时间】:2011-01-24 03:57:26
【问题描述】:

在图像视图中,如何在 iPhone 中以编程方式添加下一个和上一个按钮(前后选项)?

【问题讨论】:

标签: ios iphone uibutton


【解决方案1】:

在图像视图中添加按钮?

如果是,代码如下

    UIButton *nextBut=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    nextBut.frame= CGRectMake(x, y, 120, 100);
    [nextBut setTitle:@"Next" forState:UIControlStateNormal];
    [nextBut addTarget:self action:@selector(nextbuttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.imageView addSubview:nextBut];
    [nextBut release];

    UIButton *prevBut=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    prevBut.frame= CGRectMake(x, y, 120, 100);
    [prevBut setTitle:@"Previous" forState:UIControlStateNormal];
    [prevBut addTarget:self action:@selector(prevbuttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.imageView addSubview:prevBut];
    [prevBut release];

x,y 是按钮在视图中的位置。

然后为按钮操作编写代码,

-(IBAction)nextbuttonAction
{
//Next Button Action
}

-(IBAction)prevbuttonAction
{
//Previous Button Action
}

编辑:

设置pickerView 委托。在如下方法代码中,

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
 imageView.image=[UIImage imageNamed:[imageArray objectAtIndex:row]];
}

【讨论】:

  • 在此处发布您的一些代码,以便我回答。
  • 检查我编辑的答案并确保您为pickerView设置了委托
【解决方案2】:

将所有图像放入一个数组(let imageArray)并为当前图像获取整数类型的全局变量(let curIndex) 分别递增和递减全局变量nextbuttonAction和prevbuttonAction

并从索引 curIndex ([imageArray objectAtIndex:curIndex]) 中的 imageArray 更改 imageview 控件的图像

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多