[英]Ios - change image of UIImageView when pressing a UIButton
So I am developing my first app in iOS and I don't know how to fix this. 所以我正在用iOS开发我的第一个应用程序,但我不知道如何解决。 In the same view I have: - two
buttons
inside a stackView
(that I modified) - an UIImageView
Storyboard View 在同一视图中我有: -两个
buttons
一内部stackView
(即我改性) -一个UIImageView
故事板视图
Depending on which button I press, I want the image to change in UIImageView
. 我希望根据要按下的按钮在
UIImageView
更改图像。 However, bc the buttons are in the stackView
, I can't push an IBAction
from them to the controller. 但是,由于按钮在
stackView
,所以我无法将IBAction
从按钮推入控制器。
I changed the image based on the button inside the function didViewLoad() , but as I learned, that function is only called once, so when when I press the buttons
in the app it has no more effect in the UIImageView
. 我根据didViewLoad()函数中的按钮更改了图像,但据我了解,该函数仅被调用一次,因此当我按应用程序中的
buttons
时,它在UIImageView
不再起作用。
This is my controller for this view Controller Code 这是此视图的控制器
And this is the beginning of the code for the subclass of the UIStackView UIStackView subclass 这是UIStackView子类的代码的开头
In your case, you also have the option of using Segmented Controller. 在您的情况下,您还可以选择使用分段控制器。
Place the segmented controller in your View Controller in the StoryBoard. 将分段控制器放在StoryBoard的View Controller中。
Make an outlet of the Segment Controller 出口段控制器
@IBOutlet var segmentedController: UISegmentedControl!
Make an IBAction
Function for Value Changed
event in the Segmented Control. 为分段控件中的
Value Changed
事件创建IBAction
函数。 Now whenever the selected tabs are changed you will be notified. 现在,无论何时更改选定的选项卡,您都会收到通知。
@IBAction func SegmentTapped(_ sender: Any) {
switch segmentedController.selectedSegmentIndex {
case 0:
// Change Image Here
case 1:
// Change other image here
default:
// Statement to execute if no other case is present
}
}
Using Segmented Controller would be a good idea for this case. 对于这种情况,使用分段控制器将是一个好主意。 Thank you for that.
谢谢你
For the more general approach of how to make things happen with what I had already written, this post ( Open a storyboard view with a stackview button in Swift 3 and Xcode ) was very useful and also taught me how to properly use protocols and delegates. 对于使用我已经写的东西使事情发生的更通用方法,这篇文章( 在Swift 3和Xcode中使用stackview按钮打开一个Storyboard视图 )非常有用,并且还教会了我如何正确使用协议和委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.