I have two buttons in my xib file. This is my interface file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *testButtonOut;
#property (strong, nonatomic) IBOutlet UIButton *button2;
- (IBAction)testButton:(id)sender;
- (void)buttonReleased;
#end
These are the implementations of two functions in my implementation file
- (IBAction)testButton:(id)sender {
button2.highlighted=YES;
[testButtonOut addTarget:self action:#selector(buttonReleased) forControlEvents:UIControlEventTouchCancel];
}
- (void)buttonReleased{
button2.highlighted=NO;
}
When I click on the testButton and release, it does not trigger the buttonReleased function (I used breakpoint to check it), but if I change UIControlEventTouchCancel to UIControlEventTouchUpInside it does trigger the buttonReleased function when I click on it but I dont want to trigger it on button click I want to trigger it on button release, I also tried with TouchUpOutside but that didnt trigger as well. Which event should I use so that it calls the buttonReleased function when I release the click from the button event?
Instead of
"UIControlEventTouchCancel" use
"UIControlEventTouchUpInside."
Related
I have a ViewController that populates a couple of sliders as so:
// ViewController.h
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UISlider *aSlider;
#property (weak, nonatomic) IBOutlet UISlider *anotherSlider;
#end
// ViewController.m
#interface ViewController ()
#end
#implementation ViewController
-(void)resetSliders
{
NSLog(#"Resetting sliders sl1: %# sl2: %#", self.aSlider, self.anotherSlider);
[self.aSlider setValue:0.5f animated:NO];
[self.anotherSlider setValue:0.5f animated:NO];
}
- (IBAction)setOneSlider:(UISlider *)sender {
NSLog(#"Setting aSlider to %f", sender.value);
self.sl1Val = sender.value;
[self doAThing];
}
- (IBAction)setTwoSlider:(UISlider *)sender {
NSLog(#"Setting anotherSlider to %f", sender.value);
self.sl2Val = sender.value;
[self doBThing];
}
#end
The calls to setOneSlider and setTwoSlider are wired to the value changed events of the two sliders. That works perfectly well. Unfortunately, I have not connected the sliders to their programmatic handles, because the call to resetSliders yields no change in the UI, even with a call added to setNeedsDisplay on the two slider elements.
When resetSliders is called, the log shows:
2015-04-28 12:22:19.608 myApp[4489:681127] Resetting sliders sl1: (null) sl2: (null)
Can anyone indicate to me how to set the self.aSlider and self.anotherSlider with the correct object handles?
To add some color: I have dragged the UI representation of the sliders to their respective #property declarations, and I can see that they have filled-in grey circles in the margin, indicating that they've been wired. The Connections Inspector shows both a "Value Changed" event trigger and a Referencing Outlet connecting myApp to the slider(s).
I'm extremely new to IOS programming and am reaching the limits of learning-by-failing.
The properties should be defined inside the #interface blocks. Yours are currently outside. Additionally, they should probably be weak instead of retain, and no need for the #synthesize statements or the ivars.
Lastly, when you reference them in code, you should do so as self.aSlider, not just aSlider.
It sounds like you still need to connect these outlets in Interface Builder:
#property (retain, nonatomic) IBOutlet UISlider *aSlider;
#property (retain, nonatomic) IBOutlet UISlider *anotherSlider;
Also you don't need to declare properties and ivars for the same sliders. Just declare and use the properties you already have.
#interface ViewController : UIViewController
and
-(void)resetSliders
{
// note the addition of 'self.' below
NSLog(#"Resetting sliders sl1: %# sl2: %#", self.aSlider, self.anotherSlider);
[self.aSlider setTo:0.5f animated:NO];
[self.anotherSlider setTo:0.5f animated:NO];
}
Edit re-wrote question:
Hi there I'm working on another student's project (taking over) there's no main storyboard, only a viewonctroller.Xib under resources a Viewcontroller.h and .m
I'm trying to add new buttons but they aren't displaying. I linked the new buttons to the viewcontroller.h and .m file with no actual code in the code such as
-(IBAction)button { empty }
in the .h file it's
IBOutlet UIButton *button;
The button's not displaying in the simulator however, Also the former student has every button in the app displayed in the same window and just uses the setHidden function to distinguish between menu's in game. I thought that maybe the button was just behind other buttons but when I go to editor-> send to front the option is grayed out.
edit Sample code of ViewController.H
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <QuartzCore/QuartzCore.h>
// Game Center -- ignore and or delete
#import <GameKit/GameKit.h>
#import "GameCenterManager.h"
#interface SideWinderViewController : UIViewController <AVAudioPlayerDelegate, /*ADBannerViewDelegate,*/ UIActionSheetDelegate, GKLeaderboardViewControllerDelegate,
GKAchievementViewControllerDelegate, GameCenterManagerDelegate> {
// Game Center
GameCenterManager *gameCenterManager;
int64_t currentScore;
NSString* currentLeaderBoard;
IBOutlet UILabel *currentScoreLabel;
// End Game Center
IBOutlet UIImageView *ballpng;
IBOutlet UILabel *displaycredit2;
IBOutlet UILabel *displaycredits;
IBOutlet UIButton *easy;
IBOutlet UIButton *medium;
IBOutlet UIButton *hard;
IBOutlet UILabel *displayhighscore;
IBOutlet UILabel *Howtoplay;
IBOutlet UIButton *back;
IBOutlet UIButton *info;
IBOutlet UIButton *playbutton_mainmenu;
IBOutlet UIImageView *winder1;
IBOutlet UIImageView *socket1;
IBOutlet UIImageView *wheelcontrol2;
IBOutlet UILabel *seconds;
IBOutlet UILabel *scoreLabel;
IBOutlet UIButton *Restart;
IBOutlet UIButton *HIGHSCORE;
IBOutlet UIButton *pausebutton;
IBOutlet UIButton *resumebutton;
IBOutlet UIButton *gameStartButton;
IBOutlet UIImageView *mainmenu;
IBOutlet UIButton *credits;
IBOutlet UIButton *returnt
sample code for .m
#import <UIKit/UIKit.h>
#import "SideWinderViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "Ball.h"
#import <AVFoundation/AVAudioPlayer.h>
// Game Center -- ignore and or delete
#import "AppSpecificValues.h"
#import "GameCenterManager.h"
// End Game Center
#implementation SideWinderViewController
-(void)viewDidLoad {
[Restart setHidden:NO];
...
Thats enough to show a button
Check
Xib shown is the right one
Is tht button is covered by some other view added via code
Is the button property hidden checked or unchecked
Check button Frame
I have a Storyboard with a UITableViewController. In this controller, I have static cells where one of them is a subclass of UITableViewCell. In the IB I added a UIButton to this static cell and connected it to an IBAction in the UITableViewCell subclass. However, the IBAction is never called. Why doesn't this work? How can I achieve the desired behaviour where a button inside a cell triggers an event through the IB?
.h
#import <UIKit/UIKit.h>
#interface BSWorkoutsCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UIButton *button;
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *buttons;
- (IBAction)tapButton:(id)sender;
#end
.m
- (IBAction)tapButton:(id)sender{
if (!_buttonsHidden){
[self animateHideButtons:(UIButton *)sender];
_buttonsHidden = YES;
}
}
Images
I realised that the UITableViewCell had User Interaction Enabled unchecked in the IB. This made any subviews inside it not interactive, thus the buttons not actually being pressed, and the IBAction not being called.
Once I checked the box, everything worked fine.
I have 3 UIButton created through the Interface Builder of Xcode.
Have the IBOutlet and IBAction defined in the Controller.h like this:
#property (strong, nonatomic) IBOutlet UIButton *btnToday;
#property (strong, nonatomic) IBOutlet UIButton *btnToday_less_1;
#property (strong, nonatomic) IBOutlet UIButton *btnToday_less_2;
- (IBAction) setBtnToday: (UIButton *) sender;
- (IBAction) setBtnToday_less_1: (UIButton *) sender;
- (IBAction) setBtnToday_less_2: (UIButton *) sender;
In my Controller.m have this:
#synthesize btnToday;
#synthesize btnToday_less_1;
#synthesize btnToday_less_2;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[btnToday setTitle:#"Today" forState: UIControlStateNormal];
[btnToday_less_1 setTitle:#"Yesterday" forState: UIControlStateNormal];
[btnToday_less_2 setTitle:#"Day before yesterday" forState: UIControlStateNormal];
}
The above code works OK, but if I implement the IBAction it does not.
The implementation looks like this:
-(IBAction)setBtnToday:(UIButton *)sender{ /* a method call */ }
-(IBAction)setBtnToday_less_1:(UIButton *)sender{ /* a method call */ }
-(IBAction)setBtnToday_less_2:(UIButton *)sender{ /* a method call */ }
If I do a NSLog of any of the IBOutlet they return nil. If remove the code of the IBAction from the Controller.m file it starts working again.
What am i doing wrong?
note: does not work means it compile without Errors and run but the text I am trying set on the UIButton are not updated.
You can't override setBtnToday:, setBtnToday_less_1:, and setBtnToday_less_2: like that because those are exactly the setter methods that the runtime is relying on to set up the buttons you configured in your xib file.
When you create a (non readonly) property the compiler synthesizes both a "getter" and "setter" method for your property. By default the getter method is - (<property type>)<property name> and the setter is - (void)set<CamelCase property name>. And those IBAction methods you posted are exactly the setters for your three button properties. By writing them explicitly (and making them do nothing) you're preventing the compiler from synthesizing the default setter implementations and that in turn prevents the xib loading code from working in the expected way.
If you're trying to implement methods to respond to the buttons being tapped you should just name them something else, like - (IBAction)btnTodayTapped:(id)sender, - (IBAction)btnToday_less_1_tapped:(id)sender, etc. Alternatively you could make one method, - (IBAction)buttonTapped:(id)sender and have that method inspect the sender to determine which button was tapped.
I have a little problem that my buttons doesn't move when I am using a code that should move the button.
ViewControlle.h:
#property (strong, nonatomic) IBOutlet UIButton *button1;
#property (strong, nonatomic) IBOutlet UIButton *button2;
ViewController.m:
#implementation GameViewController
#synthesize button1,button2;
code which should move the buttons:
button1.center=CGPointMake(button1.center.x, button1.center.y-10);
button2.center=CGPointMake(button2.center.x, button2.center.y+10);
why does the code not work or am I doing something wrong?
Because autolayout did not happen before view did load it will reset your locations.
when - (void)viewDidLayoutSubviews gets called its done so place your code here.
If you are using AutoLayout for ViewController than It's not work.
Uncheck the AutoLayout for ViewController.