Textfield return (null) no matter what - objective-c

I have searched stackoverflow for similar problems and have found some, but they did not resolve my problem. I have 3 textfields (name,address,phone) from which I want to get the text. I have declared them in the (.h) file, also the #property, and then #synthesized them in the (.m) file. I have a IBAction for a button declared in the (.h) file and linked up correctly. Now when I push this button I want to get the values from the textfields but NSLog shows they are all (null) even before I do anything with the textfields. Its very simple code, i can't understand why it returns null.
//CoreDataViewController.h
#interface coreDataViewController : UIViewController {
UITextField *name;
UITextField *address;
UITextField *phone;
UILabel *status;
}
#property (strong, retain) IBOutlet UITextField *name;
#property (strong, retain) IBOutlet UITextField *address;
#property (strong, retain) IBOutlet UITextField *phone;
#property (strong, retain) IBOutlet UILabel *status;
- (IBAction) saveData;
#end
//CoreDataViewController.m
#import "coreDataViewController.h"
#import "AppDelegate.h"
#implementation coreDataViewController
#synthesize name=_name;
#synthesize phone=_phone;
#synthesize address=_address;
#synthesize status=_status;
- (void) saveData
{
NSLog(#"Name %# Address %# Phone %#",self.name.text,self.address.text,self.phone.text);
...some more code (commented out)....
}

IMO you have incorrectly synthesized iVars.
You use syntax:
synthesize name=_name;
but in the .h file you have:
#interface coreDataViewController : UIViewController {
UITextField *name;
UITextField *address;
UITextField *phone;
UILabel *status;
}
Change this part to:
#interface coreDataViewController : UIViewController {
UITextField *_name;
UITextField *_address;
UITextField *_phone;
UILabel *_status;
}

Just change below code
write synthesize like this
#synthesize name;
#synthesize phone;
#synthesize address;
#synthesize status;
ratherthen
#synthesize name=_name;
#synthesize phone=_phone;
#synthesize address=_address;
#synthesize status=_status;

Related

unable to hook to a ui view inside interface

I am using THEOS, and I know how to hook to methods and override and stuff. But now what I want to do is, I want to access the UI views that are declared within (#interface).
Here is the header file I am referring to:
#class NSString, UILabel, UINavigationBar, UISwitch;
__attribute__((visibility("hidden")))
#interface SettingsViewController : UIViewController <UINavigationBarDelegate, UIBarPositioningDelegate>
{
UINavigationBar *navBar;
UISwitch *alertSwitch;
UISwitch *lightSwitch;
UISwitch *lockSwitch;
UISwitch *infoSwitch;
UILabel *alertLabel;
UILabel *lightLabel;
UILabel *lockLabel;
UILabel *showInfoLabel;
UILabel *infoLabel;
}
#property(retain, nonatomic) UILabel *infoLabel; // #synthesize infoLabel;
#property(retain, nonatomic) UILabel *showInfoLabel; // #synthesize showInfoLabel;
#property(retain, nonatomic) UILabel *lockLabel; // #synthesize lockLabel;
#property(retain, nonatomic) UILabel *lightLabel; // #synthesize lightLabel;
#property(retain, nonatomic) UILabel *alertLabel; // #synthesize alertLabel;
#property(retain, nonatomic) UISwitch *infoSwitch; // #synthesize infoSwitch;
#property(retain, nonatomic) UISwitch *lockSwitch; // #synthesize lockSwitch;
#property(retain, nonatomic) UISwitch *lightSwitch; // #synthesize lightSwitch;
#property(retain, nonatomic) UISwitch *alertSwitch; // #synthesize alertSwitch;
#property(retain, nonatomic) UINavigationBar *navBar; // #synthesize navBar;
- (void).cxx_destruct;
- (void)didReceiveMemoryWarning;
- (void)dismissSettingsViewController;
- (long long)preferredStatusBarStyle;
- (void)showDevInfo;
- (void)infoSwitchSwitched;
- (void)lockSwitchSwitched;
- (void)lightSwitchSwitched;
- (void)alertSwitchSwitched;
- (void)viewDidLoad;
- (long long)positionForBar:(id)arg1;
// Remaining properties
#property(readonly, copy) NSString *debugDescription;
#property(readonly, copy) NSString *description;
#property(readonly) unsigned long long hash;
#property(readonly) Class superclass;
#end
I want to access the UI views under #interface (Ex: UISwitch *alertSwitch;).
How can I access it? Please help I am trying to figure it out since three days :(. Any help or suggestion is very very appreciated.
I hope this help
%hook SettingsViewController
-(void) WhereEverYouWantToRunYourCode {
self.alertSwitch = ....
}
%end

How to add a UIView to an UIAlertView?

I have to create a custom AlertView with a view that contain some label. I create the .h and .m of CustomAlert
this is the CustomAlert.h.
#import <Foundation/Foundation.h>
#interface CustomAlert: UIView
#property (nonatomic, weak) IBOutlet UILabel *ok;
#property (nonatomic, weak) IBOutlet UILabel *ok1;
#property (nonatomic, weak) IBOutlet UILabel *ok2;
#property (nonatomic, weak) IBOutlet UILabel *ok3;
#property (nonatomic, weak) IBOutlet UILabel *ok4;
#property (nonatomic, weak) IBOutlet UILabel *ok5;
#end
and this is the CustomAlert.m.
#import "CustomAlert.h"
#implementation CustomAlert
#synthesize ok = _ok;
#synthesize ok1 = _ok1;
#synthesize ok2 = _ok2;
#synthesize ok3 = _ok3;
#synthesize ok4 = _ok4;
#synthesize ok5 = _ok5;;
#end
I also create the xib with just a view and i connect the label with all the "ok".
Now, i want to add this View to an AlertView with just an Ok button. How can i do it? I want to use it with ios 7 and 6.
Thanks!!
In iOS 7 you should not munge a UIAlertView like this - and there is no need, because thanks to custom view transitions you can make your own small presented view in front of your interface, so that it looks and acts like a UIAlertView but can contain anything you like.
I've written an online tutorial on how to do this:
http://programming.oreilly.com/2014/01/transcending-uialertview-on-ios-7.html
And there's a downloadable github project that goes with it:
https://github.com/mattneub/custom-alert-view-iOS7

UITextField bind

I can't figure out why that code is not working for me.
ViewController.h
...
#property (nonatomic, copy) UITextField *textField;
...
ViewController.m
-(void)viewDidLoad
{
[self.textField addTarget:self
action:#selector(textIsChanged:)
forControlEvents:UIControlEventEditingChanged];
}
-(void)textIsChanged:(id)sender;
{
NSLog(#"Changed");
}
When I type something in the textField textIsChanged method is never invoked.
You should declare textField as an IBOutlet like this:
#property (nonatomic, retain) IBOutlet UITextField *textField;
or, if you are using ARC (Automatic Reference Counting):
#property (nonatomic, strong) IBOutlet UITextField *textField;
and bind it from the xib file in interface builder.

Expected { before interface

Why am I getting the above compilation error in all of my source files after creating editing this file:
#import <UIKit/UIKit.h>
#protocol FlipsideViewControllerDelegate;
#interface FlipsideViewController : UIViewController {
id <FlipsideViewControllerDelegate> delegate;
UITextField *textField;
}
#property (nonatomic, retain) IBOutlet UITextField *textField;
#property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
-(IBAction) textChanged:(id) sender;
After editing my flipsidecontroller.h to look like this. I get the error in all of my other source files. Like this one:
// MainViewController.h
// MVC
//
// Created by Nick Martin on 3/31/11.
// Copyright 2011 Nick. All rights reserved.
//
#import "FlipsideViewController.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
UILabel *label;
}
#property (nonatomic, retain) IBOutlet UILabel *label;
- (IBAction)showInfo:(id)sender;
#end
It is complaining about the interface declaration for each of my controllers???
Thanks in advance for the help!
Update - put the textChanged event into the interface
// FlipsideViewController.h
// MVC
//
// Created by Nick Martin on 3/31/11.
// Copyright 2011 Nick Martin. All rights reserved.
//
#import <UIKit/UIKit.h>
#protocol FlipsideViewControllerDelegate;
#interface FlipsideViewController : UIViewController {
id <FlipsideViewControllerDelegate> delegate;
UITextField *textField;
}
#property (nonatomic, retain) IBOutlet UITextField *textField;
#property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
-(IBAction) textChanged:(id) sender; //DOH!!!
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
Your textChanged: method isn't in an interface or protocol. Did you mean to include it in FlipsideViewController or FlipsideViewControllerDelegate?
You have this:
-(IBAction) textChanged:(id) sender;
but it seems to have fallen outside of your #protocol after the #end.

why wont it recognize the #properties from header file from main file?

UIView.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface UIView : UIResponder {
IBOutlet UILabel *endLabel;
IBOutlet UIButton *goButton;
IBOutlet UITextField *textBox1;
IBOutlet UITextField *textBox2;
#property(nonatomic, retain) UILabel *endLabel;
#property(nonatomic, retain) UIButton *goButton;
#property(nonatomic, retain) UITextField *textBox1;
#property(nonatomic, retain) UITextField *textBox2;
}
- (IBAction)goButtonClicked;
#end
UIView.m
#import "UIView.h"
#implementation UIView
#synthesize textBox1, goButton;
#synthesize textBox2, goButton;
#synthesize textBox1, endLabel;
#synthesize textBox2, endLabel;
#synthesize goButton, endLabel;
- (IBAction)goButtonClicked {
}
#end
Going a bit crazy with the #synthesizes, are we? I do believe that your main problem here is that #property declarations need to be after the closing } of #interface.
I'm surprised the compiler didn't throw up a red flag the size of Greenland, tho'.
Additionally, you probably meant to make a custom subclass of UIView; I'll use MyView.
//MyView.m -- correct synthesize declaration
#synthesize textBox1, goButton, textBox2, endLabel;
//MyView.h -- correct interface declaration
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface MyView : UIView {
IBOutlet UILabel *endLabel;
IBOutlet UITextField *textBox1;
IBOutlet UITextField *textBox2;
IBOutlet UIButton *goButton;
}
#property(nonatomic, retain) UIButton *goButton;
#property(nonatomic, retain) UILabel *endLabel;
#property(nonatomic, retain) UITextField *textBox1;
#property(nonatomic, retain) UITextField *textBox2;
#end
The first problem is that you're naming your class UIView, which already exists in UIKit. See #Williham's advice on resolving this.
You only need one #synthesize per property, and when the property name matches the instance variable name, you should only need to do something like this in your .m file:
#synthesize endLabel;
#synthesize goButton;
#synthesize textBox1;
#synthesize textBox2;
Also, you're likely to run into problems getting your IBAction method to work. To use a method for a target-action linkage, it must have a return type of IBAction (which you have correct) and accept an id parameter representing the sender. The canonical method signature looks like this:
- (IBAction) goButtonClicked:(id)sender;
I'd actually recommend a method name that's not explicitly tied to the button that invokes it, especially since there could be other ways to invoke the same action. (For example, if you were writing a desktop application, a key equivalent or menu command could do the same thing.)