Xcode IBAction and Outlets - objective-c

I am using same action for multiple uibuttons. Is it possible to get which button is the sender?
E.g. Button 1 has been tagged with outlet1 and button2 with outlet2 etc,
When these buttons are pressed, it will call the same action but based on what the sender is, I want to add my logic

//BY DEFAULT THERE IS A TAG OF BUTTON WHICH IS 0
if(yourButton.tag==0){
//do something
yourButton.tag=1;
}else if(yourButton.tag==1){
//do something
yourButton.tag=0;
}
YOU CAN PERFORM DOUBLE ACTION IN SINGLE OUTLET .
THANK YOU

Related

Pass button Selected state to another view Controller

i have Two View Controllers. 1st controller have 1 button. i use
button.selected = YES;
i'm using If statement to do some custom functions if the button is selected state.
-(IBAction)play:(id)sender;
{
if (button.selected)
{
custom code
}
}
when i use this button in same view controller with play button it works. but i want to move the button to another view controller. but it's not working.
question is how to pass button select value to the other view controller?
I don't know exactly what you're trying to achieve. Do you just dragged a button from one interface builder window to another and wonder why that button doesn't trigger 'play:' in VC 2 or do you want to let a second view controller know about the state of a button that is a subview of view controller one?
1) If you just dragged the button from one IB window to another check the connections again. That's like the most usual problem here.
2) If you just wanna let VC 2 know about the state of the button that's currenty on VC 1 one way would be adding a BOOL to VC 2 that represents that button's state. Whenever the button get's triggered it is responsible for updating that BOOL.

Check if spacebar is pressed inside a NSTextView

I have a NSTextView where I need to check when a specific button is pressed. Using ModifierKeyFlags I can check if buttons like shift and control is pressed, but I need to check if SPACE is pressed. Therefore, I cannot use the ModifierFlags (cause it can check some buttons, excluding the spacebar)
So I need a check, that notifies me everytime I press the spacebar in the textfield in my application. Any thoughts? I think it needs to be something like this:
if(spacebar is pressed) {
Dlog(#"give me a notification");
}
Use NSText's delegate.
-(void)textDidChange:(NSNotification *)notification
{
//Check if it has added a spacebar
}

Method to track which was the last button pressed of a specific type

I have a simple calculator app. I have 4 operator buttons and the calculator number digits. I want to keep track of which operator button was the most recent one pressed.
When any operator button is pressed, its label changes color from white to orange. Then when a digit button is pressed, the label reverts back to white. What I want to do is have the label turn back to orange if the user presses clear before pressing another operator or equals.
My idea is to have an if statement inside of the clearDisplay method, for example:
If (lastOperatorPressed == button1) {
Change its titleColor…
So if the user inputs a series of numbers and then hits +, the + button will be registered as the most recent operator button pressed and then if they continue inputting the next series of numbers and before pressing another operator or equals and press clear, my if statement inside clearDisplay will trigger.
I am unable to figure out a way to keep track of which operator button was most recently pressed.
Any help greatly appreciated.
You could process all of the button presses through the same method, where (id)sender is the argument value that is passed. Then you can do your button-specific processing by comparing (with a cast) the value of (UIButton*)sender to specific button object values. Within this single method, now you always have the value of sender, which is the last button pressed. Assign it to a varible like lastButtonPressed = sender;
Update: Sorry, that should be like lastButtonPressed = (UIButton*)sender;
Here are some more details:
If you use Interface Builder, connect each button to a button handler method in your view controller. (I don't use Interface Builder so much, but I think you want to connect the touch-up inside event to the button handler method).
If you don't use Interface Builder, then you can add this line where you initialize your ivars in the view controller. Assume your button handler is called buttonHandler: Do this for every one of your buttons:
[oneOfYourButtons addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventTouchUpInside];
//... include this for each button ...
[anotherOneOfYourButtons addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventTouchUpInside];
In your header file, create an instance variable called lastButtonPressed (for example). Initialize it to nil in your implementation file where you initialized your other ivars.
Then add a method like this:
- (void)buttonHandler:(UIButton*)sender{
if (sender == button1) {
//... do things for button1
// what you do here may depend on the value of lastButtonPressed, like this, for example
if (lastButtonPressed == someButton) {
//... do something
//... you may want to reset the value of lastButtonPressed
lastButtonPressed = nil;
}
else if (sender == button2) {
//... do things for button2
}
else if (sender == button3) {
//... do things for button3
}
else
//... Repeat for all the buttons you want to handle. The compiler will optimize this so don't worry about that
//...
// probably at the end, you want to save the current button as the last button pressed
lastButtonPressed = sender;
}

UIPageControl value changed not firing

I have an issue with the UIPageControl. I simplified the issue for clarity below:
I put a button and a UIPageControl in my app with Interface builder and put the following in the code:
- (IBAction)tappedButton:(id)sender{
self.pageControl.currentPage = 3;
NSLog(#"Tapped Button");
}
- (IBAction)changePage:(id)sender{
NSLog(#"PAGE Changed!!!!");
}
I attached the Value Changed action to the pageController via Interface Builder.
When I tap on the button, I see the output "Tapped Button" and the third dot is highlighted... but the changePage method is never called.
Any ideas?
The changePage: method will only be called when the users request a change of page using the UIPageControl. It won't be called when the user taps the button. If you want to call the changePage: method when the button is pressed called it explicitly:
[self changePage: nil];
UIPageControl doesn't actually change pages for you. You have to write that code, inresponse to the valueChanged event. So, you want to connect that event of your UIPageControl object to your output (tappedButton: I guess), and then call your changePage: method to actually change the UI. Does that help?
There are some example projects referenced from the Apple docs that use UIScrollView and UIPageControl together. Check 'em out.

How to identify button press among several button in objective c

I have 4 button (b1,b2,b3,b4) and a label (lab).Now I want to display button title in label when a particular button is pressed.i did it with four (IBAction) method one for every button.But i want to do it with 1(IBAction) method.so the problem is to to how to identify which button is pressed??? i knew a method something like "getBytitle" method.But i need better solution.Can anyone help??? I also need answer about how to identify button in segment control.Advanced thanx for reply.
Have a look in IB, the tag field of the button attibutes might be what you are looking for. Set each of the buttons you want to detect with a different integer tag value, and then set their IBActions to the same method. Now you can check which button was pressed by checking for the tag field in the sender
- (IBAction) buttonPressed: (id) sender
{
switch ( ((UIButton*)sender).tag ){
case 1:
<something>
break;
case 2:
<something else>
break;
default:
<default something>
}
}
The button that triggers the action gets passed as the sender. Your method probably looks a bit like this:
- (IBAction) buttonPressed: (id) sender;
The sender is the button, so that if you want to display the button title in a label, all you have to do is this:
- (IBAction) buttonPressed: (id) sender
{
label.text = [sender currentTitle];
}
That should be it.