Calling a function from a button - ios

Can anyone help me to understand what is going wrong with my function below pls?
I'm fairly new to native IOS dev.
I wanted to implement a side panel into our app using this tutorial (http://www.appcoda.com/ios-programming-sidebar-navigation-menu/).
All that works fine and is straightforward enough - but I wanted to trigger the reveal functionality via a standard button rather than a dynamic button in the header - to suit our design..
The original code is as follows -
#import "SWRevealViewController.h"
// Change button color
_sidebarButton.tintColor = [UIColor colorWithWhite:0.96f alpha:0.2f];
// Set the side bar button action. When it's tapped, it'll show up the sidebar.
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = #selector(revealToggle:);
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
to replicate that functionality triggered by a button I added the following to the .h file -
#property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
#property (weak, nonatomic) IBOutlet UIButton *revealLeftBtn;
- (IBAction)btnClickRevealL:(id)sender;
and the following in my .m file
- (IBAction)btnClickRevealL:(id)sender {
[self.revealViewController];
[#selector(revealToggle:)];
}
I get the error 'suspected identifier' for the above lines - I dont understand how the functions triggered with the previous method and not with the above - can anyone help?
Cheers
for reference this is the entire .M file -
#import "MainViewController.h"
#import "SWRevealViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"";
// Change button color
_sidebarButton.tintColor = [UIColor colorWithWhite:0.96f alpha:0.2f];
[self.navigationController.navigationBar setTranslucent:TRUE];
// Set the side bar button action. When it's tapped, it'll show up the sidebar.
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnClickRevealL:(id)sender {
[self.revealViewController];
[#selector(revealToggle:)];
}
#end

This line:
(IBAction)btnClickRevealL:(id)sender {
[self.revealViewController];
[#selector(revealToggle:)];
}
Does nothing.
I guess this is what you want to do:
[self.revealViewController revealToggle:sender];
When you set the target and the action of a button, you set which class is doing the action (target) and what method is called (action).
that's the equivalent of doing [target action]

- (IBAction)logInBtnClicked:(id)sender {
//Remove WhiteSpace from start of uitextfield
self.emailIdTxtFldRef.text = [self.emailIdTxtFldRef.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
self.passwordTxtFldRef.text = [self.passwordTxtFldRef.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
if ([self.emailIdTxtFldRef.text isEqualToString:#""]||[self.passwordTxtFldRef.text isEqualToString:#""]) {
[self showMessage:#"All fields are mandatory" withTitle:#"Error"];
}else{
SWRevealViewController *revealVC = [self.storyboard instantiateViewControllerWithIdentifier:#"swRevealViewContoller"];
revealVC.delegate=self;
[[UIApplication sharedApplication]keyWindow].rootViewController = revealVC;
}
}

Related

Button click not working in iOS

I am building an ANE which will start a new View which contains a label and button. I was successful in adding view as bundle and view controller in my .a file.
I am starting my view as a subview. And later when I click button, it should do some stuffs and change the label text. I can display the view using below code but my button click event is not working.
--- Code to start view ---
My ViewController class name is 'VideoViewController' and bundle containing xib name is 'ViewBundle.bundle'
UIApplication *app = [UIApplication sharedApplication];
UIViewController *myViewController;
NSBundle * mainBundle = [NSBundle mainBundle];
NSString * pathToMyBundle = [mainBundle pathForResource:#"ViewBundle" ofType:#"bundle"];
NSAssert(pathToMyBundle, #"bundle not found", nil);
NSBundle *bundle = [NSBundle bundleWithPath:pathToMyBundle];
myViewController = [[VideoViewController alloc] initWithNibName:nil bundle:bundle];
[app.keyWindow addSubview:myViewController.view];
--- VideoViewController.h ---
#import <UIKit/UIKit.h>
#interface VideoViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *textLabel;
#property (strong, nonatomic) IBOutlet UIButton *clickBtn;
- (IBAction)BtnTapped:(id)sender;
- (IBAction)BtnTappped:(UIButton *)sender;
#end
--- VideoViewController.m ---
#import "VideoViewController.h"
#interface VideoViewController ()
#end
#implementation VideoViewController
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self logMessage:#"From did load"];
[_clickBtn setUserInteractionEnabled:YES];
[_clickBtn setTitle:#"Click Here" forState:UIControlStateNormal]; // button text changes here
}
#pragma mark - Public
- (IBAction)BtnTapped:(id)sender {
[_textLabel setText:#"Btn Tapped"];
}
- (IBAction)BtnTappped:(UIButton *)sender {
[_textLabel setText:#"Btn Tappped"];
}
#pragma mark - Private
- (void)logMessage:(NSString *)msg {
NSLog(#"%#", msg);
[_textLabel setText:msg];
}
#end
I tried to give touch event programmatically, but still button click event doesn't work.
Can anyone please help to sort out this issue?
Please check your clickButton is on its superView
Issue was with view initialisation. Replace [app.keyWindow addSubview:myViewController.view]; with
[app.keyWindow.rootViewController presentViewController:myViewController animated:false completion:nil]; solved my issue.

How to prevent overlap when changing a button's text?

I have the following code to change the text of the button from "Button" to "Press Me!":
[self.aButton setTitle:#"Press Me!" forState:UIControlStateNormal];
However, when I run the simulator, it shows the text of both and it overlaps. I tried constricting the button, but then after I ran the simulator again, there were two buttons, one with the default text of "Button", and the other with "Press Me!". I also tried deleting the default text of the button from the utilities panel, but that didn't work either.
How do I fix this?
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *aButton;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.aButton setTitle:#"Press Me!" forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPressed:(id)sender {
self.view.backgroundColor = [UIColor orangeColor];
}
#end

Error in if statement while comparing UITextField's text and a string

In my app, I have a view controller which is having a search bar with UItextfield at the top and a UIImageview below that. The image view is initially hidden.
I want this image view to unhide through an if statement. The user will enter keywords into the textfield and when a certain word will match a pre defined string in the.m file, then it must show the image.
I originally had two view controllers but now I added another one (thirdviewcontroller). As I enter a word into the textfield, the simulator will direct me back to the code highlighting in green on this line:
if ([string1 isEqualToString:string2]) {
locationMap.hidden = YES;
This is .h file:
#interface ThirdViewController : UIViewController
{
IBOutlet UITextField *searchLocation;
IBOutlet UIImageView *locationMap;
}
-(IBAction)SearchGo;
#end
This is the .m file:
-(IBAction)SearchGo{
NSString *string1 = searchLocation.text;
NSString *string2= #"sydney";
if ([string1 isEqualToString:string2]) {
locationMap.hidden = YES;
}
}
It sounds like you've accidentally set up a breakpoint. Simply remove the breakpoint by clicking the blue arrow to the left of the line it breaks on.
In viewDidLoad method, use:
locationMap.hidden = YES;
In your -(IBAction)SearchGo method, use:
locationMap.hidden = NO;
OR for your searchGo method:
-(IBAction)SearchGo{
if ([searchLocation.text isEqualToString:#"sydney"]) {
locationMap.hidden = NO;
}else {
//implementation
}
}
I am guessing, you have attached the IBAction with your textfield,searchLocation and triggered the action specifying "Touch up Inside". This will not work for couple of reasons.
First of all, you need to implement the textFieldShouldReturn: delegate method, so that your controller knows when you press return, it should hand over the control from your text field. Then again, a you have attached your action method to your text filed, as soon as you tap on the textfield, it goes to your method and start comparing but at this point, you have typed nothing in your textfield and it fails to conform to your if condition.
the solution is to either use the have a button and attach the action method to that button. That way, after you have typed the word "sydney", and you hit on the button. It will take whatever in your textfield and compare to that.
Here is the solution-
See the extra button named "Go". Attach your method to it.
This is my .h file-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelegate>
#property(nonatomic, strong) IBOutlet UITextField *searchLocation;
#property(nonatomic, strong) IBOutlet UIImageView *locationMap;
-(IBAction)SearchGo:(id)sender;
#end
And this is the .m file-
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic, strong) NSString *string1;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark- textfield delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
self.string1 = textField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(IBAction)SearchGo:(id)sender{
NSString *string2= #"Sydney";
if ([self.string1 isEqualToString:string2]) {
self.locationMap.hidden = NO;
}
}
#end
Save the string from the textfield after your editing is done through the delegate method. Make sure, you attach the UITextFieldDelegate to your ViewController.
Alternatively, you may want to avoid all this trouble and use the UISearchDisplay controller.

iOS Popup broken after converting to ARC

Hey I converted my project to ARC automatically and the only thing I had to fix was that Xcode changed array that had a size of [8] to [4] (still don't know what was going on there)
However, I now realized I got another problem: every time I hit a button in my Popup, the app crashed if it is linked to an IBAction. If I remove the reference to the header and main file, the button is clickable, but as soon as i assign it to a method (even if the method is empty), the whole app jut freezes/crashes.
This is how I initiate my popup:
PopUpViewController *popViewController =
[[PopUpViewController alloc] initWithNibName:#"PopUpViewController" bundle:nil];
[popViewController setTitle:#"This is a popup view"];
[popViewController showInView:self.view
animated:YES];
popViewController.view.center=self.view.center;
Pretty basic stuff, I ripped it off a tutorial I found online. My header is this:
#interface PopUpViewController : UIViewController
- (IBAction)baa:(id)sender;
#property (strong, nonatomic) UIButton *ba; // I was trying to add those buttons as properties here
#property (strong, nonatomic) IBOutlet UIButton *kl; //but no luck whatsoever
#property (strong, nonatomic) IBOutlet UIView *popUpView;
#property (strong, nonatomic) IBOutlet UIView *ppp;
- (IBAction)openSomething:(id)sender;
- (IBAction)openYoutube:(id)sender;
- (IBAction)openFacebook:(id)sender;
- (void)showInView:(UIView *)aView animated:(BOOL)animated;
#end
And my main
#import "PopUpViewController.h"
#interface PopUpViewController ()
#end
#implementation PopUpViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)showAnimate
{
// not important for this question
}
- (void)removeAnimate
{
// shortened
}
- (IBAction)baa:(id)sender{
// NSLog(#"asd"); I commented it out, but even an empty method kills the app
// only if I leave a button unassigned to any method, the app "survives" a button click
}
- (IBAction)openSomething:(id)sender
{
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.test.de"]];
}
- (IBAction)openYoutube:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.youtube.com/"]];
}
- (IBAction)openFacebook:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.facebook.com/"]];
}
- (void) doSomething{
// also tried an empty method with a "new" name over here to eliminate any caching errors, no luck
}
- (IBAction)closePopup:(id)sender {
[self removeAnimate];
}
- (void)showInView:(UIView *)aView animated:(BOOL)animated
{
[aView addSubview:self.view];
if (animated) {
[self showAnimate];
}
}
- (void)viewDidLoad
{
self.view.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:.0];
self.popUpView.layer.cornerRadius = 5;
self.popUpView.layer.shadowOpacity = 1.0;
self.popUpView.layer.shadowOffset = CGSizeMake(0.2f, 0.2f);
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
Any ideas what I could do different? I referenced everything to everything else possible now and I don't even know how it looked before anymore, but I somehow think it has something to do with the referencing.
Thanks a lot in advance, Alex
I was able to figure out on my own:
In the header of my main ViewController I added
#property (strong, nonatomic) PopUpViewController *popViewController;
Which still fired the crash. But then I adjusted the popup "opening" block to
_popViewController =
[[PopUpViewController alloc] initWithNibName:#"PopUpViewController" bundle:nil];
[_popViewController setTitle:#"This is a popup view"];
[_popViewController showInView:self.view
animated:YES];
_popViewController.view.center=self.view.center;
(added the underscores) and now it's working perfectly again.
Actually I am curious why it worked before but I am not going to investigate this any further.

iOS Viewcontroller automatically "clicks" buttons

I am currently working on an iPhone App with XCode 5.1.1. I'm only getting started with apps but things went pretty well until I recently hit a wall I can't figure out how to overcome.
On the first Viewcontroller that is shown I have two buttons to switch between languages. However everytime this viewcontroller is shown the buttons seem to be automatically "clicked" that is the functions that the buttons trigger are executed. I will attach the code from the .h and .m files here:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
NSString *settingsTitle;
NSString *gerBtn;
NSString *engBtn;
}
#property (strong, nonatomic) IBOutlet UIButton *english;
#property (strong, nonatomic) IBOutlet UIButton *german;
#property (strong, nonatomic) IBOutlet UIButton *settings;
- (IBAction)setEnglish:(UIButton *)sender;
- (IBAction)setGerman:(UIButton *)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewWillAppear:(BOOL)animated
{
if([[NSUserDefaults standardUserDefaults] boolForKey:#"englishEnabled"]){
[_settings setTitle:#"Settings" forState:UIControlStateNormal];
} else {
[_settings setTitle:#"Einstellungen" forState:UIControlStateNormal];
}
NSLog(#"%hhd", [[NSUserDefaults standardUserDefaults] boolForKey:#"englishEnabled"]);
[super viewWillAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)setEnglish:(UIButton *)sender {
NSLog(#"setEnglishClicked");
[self enLangChange];
}
- (IBAction)setGerman:(UIButton *)sender {
NSLog(#"setGermanClicked");
[self deLangChange];
}
- (void) enLangChange
{
[[NSUserDefaults standardUserDefaults] setBool:true forKey:#"englishEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void) deLangChange
{
[[NSUserDefaults standardUserDefaults] setBool:false forKey:#"englishEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
#end
Now everytime I navigate to this viewcontroller also when I start the app it will log "setEnglishClicked" and "setGermanClicked" and always set the BOOL false for the key englishEnabled.
I've tried everything from deleting the buttons and putting them back in, even starting the whole project new from scratch but it always acted the same. The strange thing is that the other two buttons in this view for "Start" and "Settings" are not "clicked". It would help a ton if somebody could advice me what I'm doing wrong!
Best Regards!
J
(IBAction) is actually defined to (void) in UINibDeclarations.h.
Therefor, the methods - (IBAction)setEnglish:(UIButton *)sender and - (IBAction)setGerman:(UIButton *)sender are setters and are called when your view controller appears.
Rename them, for example to - (IBAction)didPressEnglishButton:(UIButton*)sender and it should work.

Resources