Adding a custom UIButton and action to a view in window app delegate (ios) - ios

I am working on a tab bar application which needs a global title bar with a setttings button attached. I have achieved adding an imageview to the main window, then a label, another image, and finally a button. However, I cannot get this button to fire an action. it is set up like this:
[settingsButton addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchupInside];
Then the action defined like:
-(void)settings{
NSLog(#"please do something");
}
However nothing happens, the image doesn't even change like it is being pressed. Can i not define buttons in the app delegate this way? is it because the target is not something that is control based? i tried different targets but i must not fully understand what setting a target is. thanks for any help.
P.S.- i tried setting up the method like so as well:
-(void) settings:(id)sender{
}
and calling it by replacing the action with #selector(settings:) to no avail. Thank you.

I solved it. I forgot I changed the view behind the button to a UIImageView. I can't believe I didn't think of this before since this same issue cost me 2 hours the other day. When you have an imageView always remember to set this flag if you want stuff on it:
(someImageView).userInteractionEnabled = YES;
Hopefully I save someone an hour or two of needless headaches.
P.S. Thanks for the speedy comment !

Related

Highlight UIButton when not selecting the button itself

I have a custom class here that consists of a UIButton inside of a UIView.
The goal of this class is to allow users to have a greater area (being the UIView) to select the button than just the buttons frame itself.
Now when a user taps on the view I want the buttons highlighted image to show... But the problem is, it does not.
I've read many possible solutions to this issue For Example:
Calling: [btnObject sendActionsForControlEvents:UIControlEventTouchUpInside]
This however did not change the buttons highlight.
I also tried just settings the button.highlighted = YES;
But that did not work either.
I have the images for different states properly setup (Normal and Highlighted), I'm sure of that.
I also have the gestureRecognizer working properly as the functionality is great except for the lack of highlight.
Does anybody know if I'm missing any thing special that needs to be done in order to pull off this seemingly very simple task? Surely it's been done many times.
Thank you
You were on the right track. -[UIButton setHighlighted:] is just a flag. What you need to do is call setNeedsDisplay on that button right after you change the highlighted property.
I solved my problem a little while ago and I'm not sure if Kevin Low's answer would've worked too, it very well might have.
But for some reason, a UITapGesture doesn't work well with highlighting buttons as a view transitions (That might be because I didn't call setNeedsDisplay). The gesture that ended up working was the UILongPressGesture with a 0.0 sec for minimum duration.

why cant I change button title in ios

I am new to ios, and trying to change a simple button text from implementation side in an action.
I have tried two of these: while one of them works, the other one doesnt !
self.Btn_Analyse.titleLabel.text = #"Stop Anlaysing"; // didnt work
And...
[self.Btn_Analyse setTitle:#"Deneme" forState:UIControlStateNormal]; //worked
why doesnt the first one work?
PS: I have connected the button as IBOutlet
Why are you telling us what worked?
If it worked then there isn't a problem.
Anyway, the reason the first one didn't work is because a button is not just a label. It has various states and each state can have different properties.
You set the image for the state. You set the title for the state. etc...
Then with different states you can have different properties.
UIButton sets the title on its own, so if you directly change the text in label, button changes it back.
That's because button can have different titles for different states (label can't do that), and sets the appropriate one automatcally.

UIButton style gone after segue

I'm trying to fix a problem that does not make any sense to me.
I'm applying a style to 3 of my buttons. When I click another one, there is a segue. When I dismiss the viewController, the style is not applied to my 3 previous buttons..until I click again ! I thought it was my configuration with line code that was wrong so I've deleted every customizations.
I directly configure buttons from the interface builder.
Here it my segue code even if I think it's not useful.
- (IBAction)addContact:(id)sender {
[self performSegueWithIdentifier:#"FriendsToAddFriends" sender:sender];
}
I've added some picture to show my problem :
The strangest part is when I'm back to the first page, when I click without release my click, the style comes back. It's the same for the whole 3 buttons.
I don't have any other idea to fix it. Does someone have ?
Thank you a lot in advance !
Problem solved with this code :
for (UIButton* button in buttons) {
[button.titleLabel setTextAlignment:NSTextAlignmentNatural];
}
I applied it on each following methods :
viewWillDisappear
viewWillAppear
I still don't know why this alignment change when the view will disappear.
It seems working for now.
I hope it will help someone else.

How do I link my button to the selected state?

Now, I have sort of asked this question already, but this time it's a little different. Since my last question, I have received several answers and excellent advice, and I did some extensive research as well. I am using xcode, and coding for iOS 6.
My question specifically is how do I keep my button in a temporary "selected" state when pressed? Like I want it to stay pressed through the duration of the sound and then switch back to the default image.
My button is mostly linked up through interface builder, because it's easy and less code to write. I have the sound coded in my button's IBAction to play when pressed and I have the second image that it changes to when pressed, however the Image isn't set in my code it is set in Interface Builder.
I've seen several code that seems like it would work wonders if I knew how to correctly input it. Which leads me to a second part of my question.
Can someone help me set it up?
for example:
[btnClear setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateHighlighted];
This seems like it would do the trick in my head. I just don't know which variable of mine to replace "btnClear" with and setBackgroundImage isn't showing up as a command.
On my other question I received and answer in the form of a comment that made the most sense but they didn't elaborate, it was:
"you can set image for Selected state of your button, and on touchUpInside set property selected = YES.
So I have the image I want in the selected state, and my button is linked for touchUpInside, but the only "property" or action it has is to play the sound, and because my button's images are set in Interface Builder I'm not really sure how I can set selected = YES without coding a new class which sets up the image.
Now if that's what I have to do, that's fine, but I need some guidance setting that up I am highly familiar with Objective-C it's just I'm new to these default classes in the iOS SDK I'm still getting a hold of things and having to refer to tutorials every now and again.
Here's a link to my other question if it helps:
How do I make a button stay pressed?
Again, I already have everything set up regarding the Image in Interface Builder, I'm just not sure how to fit the selected state option it into my already existing button action.
Thank you!
I think what you may be looking for is a button property:
#property (nonatomic, weak) IBOutlet UIButton *button;
(Or with a more descriptive name!) Hook this up to your button in Interface Builder. Then when you want to switch to the selected image (which you said is already in IB, right?), simply do this:
self.button.selected = YES;
And to turn back to the default image:
self.button.selected = NO;

Make green color UIActionSheet button?

I know I can access a individual button on a actionSheet using a for loop to access the particular button in the action sheet I want, but the thing is, how would I make a green button?
I know each button acts almost identically to a UIButton so how should I go upon making one of my buttons in the action sheet green?
I just need some tips or help with this as it is the last part of my app that isn't done!
Thanks!
When I have needed customizations for UIActionSheets, sometimes I find methods to make it happen simply, but more often I end up having to set up a custom view on way or another. I've added all manners of custom controls with:
[sheet addSubview: myCustomActionSheetController.view];
You might need to set the size as well.

Resources