Hide image when same button pushed - iphone

I know how to show an image when a button is pressed. But how do you "hide" the image again once the button is pressed a second time? Here is all I have so far...
-(IBAction)Light
{
LightLCD.hidden = NO;
}

Try
-(IBAction)Light
{
LightLCD.hidden = !LightLCD.hidden;
}

Like this:
- (IBAction)Light
{
LightLCD.hidden = !LightLCD.hidden;
}
This way you can toggle the visibility of the imageView.

Related

How to add a clickable button in a List without triggering NavigationLink event?

I have a button in a list item and when I click on it, the NavigationLink's event is also being triggered.
I only want to trigger the button event. How to do it?
following is my code:
ForEach(self.cares,id:\.id){ care in
HStack{
if(care.markMode == "打卡"){
Button("今日未打卡"){
}
else{
Button("新增记录"){
self.care = care
self.showSheetNewMark.toggle()
}
}
NavigationLink(destination:CareDetailView(care: care)){
Text("\(care.nickname!)的\(care.subject!)")
}
}
}
Add PlainButtonStyle for button, as in below example
Button("新增记录"){
self.care = care
self.showSheetNewMark.toggle()
}
.buttonStyle(PlainButtonStyle()) // << here !!

Swapping bar button items

I'd like to swap a bar button item, depending on a UISegmentControl which I thought would be fairly straight forward.
My initial plan was:
Create an IBAction for when the segment changes
Work out which segment we are on
Hide the appropriate button
The code was something like this
#IBAction func segmentChanged(sender: UISegmentedControl) {
UIView.animateWithDuration(0.25) { () -> Void in
if(sender.selectedSegmentIndex == 0) {
barButtonA.hidden = true
barButtonB.hidden = false
} else {
barButtonA.hidden = false
barButtonB.hidden = true
}
}
}
That hid the right button as expected, but oddly, the space where the button was still remained, so it looked really strange.
Next I saw that in the interface builder there is a parent container (UIBarButtonItems) for each of the buttons. I'm not sure why they're needed, but I hooked them up via IBOutlets so I could hide/show them in the function, but that didn't work either as they don't have a hidden property.
Finally, I tried to use those new outlets with following code:
#IBAction func segmentChanged(sender: UISegmentedControl) {
UIView.animateWithDuration(0.25) { () -> Void in
if(sender.selectedSegmentIndex == 0) {
self.navigationItem.rightBarButtonItem = self.barButtonItemA
} else {
self.navigationItem.rightBarButtonItem = self.barButtonItemB
}
}
}
But no luck! Here, it just seems to animate swapping the two button positions/order, rather than actually removing either one.
What am I doing wrong? Thanks!
Yo should combine the two function you have.
So swap them and change the hidden state.
Update:
Did you tried to first delete the navigation.barbutton.
self.navigationItem.rightBarButtonItem = nil

access a UIButton within a rightBarButtonItem

I have a navigationcontroller with a right UIBarButtonItem. I have added a UIButton to the BarButtonItem.
How can I get to the button at run time, if possible without using tags?
var myButton:UIButton?
var rightButton = navigationItem.rightBarButtonItems?.first
for view in rightButton.subviews() {
if view.iskindOfClass(UIButton) {
myButton = view
break
}
}

Cannot manually set the highlighted state of a UIButton in an IBAction method

Inside my ViewController, I have two IBOutlets signUpButton and memberButton, and both buttons are linked to same signUpOrMemberButtonPressed() IBAction func. I am trying to set highlighted property to these 2 buttons so I can do subsequent work accordingly inside my submitPressed() IBAction func. But I noticed odd behavior between signUpOrMemberButtonPressed() and submitPressed() IBActions. After tapping on either signUpButton or memberButton, the button was highlighted inside my signUpOrMemberButtonPressed(), but by the time it executed submitPressed(), debug showed it's not highlighted. Here is my signUpOrMemberButtonPressed():
#IBAction func signUpOrMemberButtonPressed(sender: AnyObject) {
var button = sender as UIButton
button.highlighted = true
if signUpButton.highlighted {
memberButton.highlighted = false
} else {
signUpButton.highlighted = false
}
if (signUpButton.highlighted) {
println("signUpButton is highlighted inside 1st button")
} else if (memberButton.highlighted) {
println("memberButton is highlighted inside 1st button")
} else {
println("nothing is highlighted inside 1st button")
}
}
My submitPressed function:
#IBAction func submitPressed(sender: AnyObject) {
if (signUpButton.highlighted) { println("signUpButton is highlighted inside 2st button") }
else if (memberButton.highlighted) { println("memberButton is highlighted inside 2st button") }
else { println("nothing is highlighted inside 2nd button")
}
When run my app, I tapped on memberButton and then tapped on submit button. Here is the log output:
memberButton is highlighted inside 1st button
nothing is highlighted inside 2nd button
Nothing was set to run between these two func calls.
What is happening here is that the button is "un-highlighting" itself (after you manually set highlighted = true).
From the documentation of the highlighted property:
UIControl automatically sets and clears this state automatically when
a touch enters and exits during tracking and when there is a touch up.
You can set the highlighted state manually but you would have to do it after the UIButton unsets the highlighted state. You would have to do this on the next run loop which you can do by using dispatch_async.
The following should work:
var button = sender as UIButton
dispatch_async(dispatch_get_main_queue()) {
self.memberButton.highlighted = button == self.memberButton
self.signUpButton.highlighted = button == self.signUpButton
}

monotouch dialog search keyboard customise

How would I change the text on the search bar induced keyboard from "search" to "cancel" or "done"
I can change the keyboard type:
((UISearchBar)TableView.TableHeaderView).KeyboardType = UIKeyboardType.CHOICE
In order to get at the ReturnKeyType -
foreach (UIView subView in ((UISearchBar)TableView.TableHeaderView).Subviews)
{
var view = subView as IUITextInputTraits;
if (view != null)
{
view.KeyboardAppearance = UIKeyboardAppearance.Default;
view.ReturnKeyType = UIReturnKeyType.Done;
}
}
This work for my use case.