xcode: TextField.text return nil no matter what is the input: [New Referencing Outlet]? - objective-c

I have a tab-bar application.
In one of the view(.xib), there is a text field and a button. The button supposed to save the text inputted in textField.
However, no matter what i typed, it always read (nil) in textField.text in debug.
When i right click the textField in .xib, i cannot drag the [New Referencing Outlet] to [File's Owner]. i can only drag the [delegate] to [File's Owner]. Does anyone have idea on why i can't do so?
Below the the .h file:
#interface AddDebtorViewController : UIViewController
{
UITextField *tDebtorName;
}
#property (retain, nonatomic) IBOutlet UITextField *tDebtorName;
-(IBAction)returnButPressed:(id)sender;
-(IBAction)addDebtorButPressed:(id)sender;
#end
Below is the .m file:
#import "AddDebtorViewController.h"
#implementation AddDebtorViewController
#synthesize tDebtorName;
- (IBAction)addDebtorButPressed:(id)sender
{
NSString *debtorName = [[NSString alloc] init];
debtorName = tDebtorName.text;
NSLog(debtorName);
}
...
#end

You must select File's Owner, press right button mouse, find your outlet and link it with the ui element.

Related

Double click on row in NSTableView doesn't display the new view

I have an os x app that uses core data.
I have 3 .xib files in my app, those are:
1. MainMenu.xib
2. MasterTableViewController.xib
3. DetailViewController.xib
When started , app displays a view that has NSTableView with couple of records in it.
I name that view MasterTableViewController
I want when user double click on the row, to hide the "master" view and to display my "detail" view. I named that view DetailViewController.
When double clicked on the row in the NSTableView in the "master" view,nothing happen, "master" view remains visible. What I want is "master" view to dissapear, and "detail" view to appear.
Here is the code that I have right now, and more explanations follows:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (nonatomic,strong) NSViewController *mainAppViewController;
#property (weak) IBOutlet NSView *mainAppView;
- (void)changeViewController:(NSInteger)tag;
#property (weak) IBOutlet NSTableView *websitesTableView;
- (void)tableViewDoubleClick:(id)nid;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "MasterTableViewController.h"
#import "DetailViewController.h"
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
- (IBAction)saveAction:(id)sender;
#end
#implementation AppDelegate
NSString *const masterTable = #"MasterTableViewController";
NSString *const detail = #"DetailViewController";
-(void)awakeFromNib {
[_websitesTableView setTarget:self];
[_websitesTableView setDoubleAction:#selector(tableViewDoubleClick:)];
}
- (void)tableViewDoubleClick:(id)nid {
NSInteger rowNumber = [_websitesTableView clickedRow];
NSTableColumn *column = [_websitesTableView tableColumnWithIdentifier:#"websiteUrl"];
NSCell *cell = [column dataCellForRow:rowNumber];
NSInteger tag = 2;
[self changeViewController:tag];
}
- (void)changeViewController:(NSInteger)tag {
[[_mainAppViewController view]removeFromSuperview];
switch (tag) {
case 1:
self.mainAppViewController = [[MasterTableViewController alloc]initWithNibName:masterTable bundle:nil];
break;
case 2:
self.mainAppViewController = [[DetailViewController alloc]initWithNibName:detail bundle:nil];
break;
}
[_mainAppView addSubview:[_mainAppViewController view]];
[[_mainAppViewController view] setFrame:[_mainAppView bounds]];
[[_mainAppViewController view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// automatically run the master table view controller
NSInteger tag = 1;
[self changeViewController:tag];
}
Now, some of you may wondering, where is the rest of the code. I have ommited the boiler plate code for the core data below in the AppDelegage.m, since it is unchanged. I used binding to make my NSTableView to work and to display my records, so MasterTableViewController.h and .m files are empty, and same is true for the DetailViewController.h and .m file.
Important note - What i can't understand here: If I change the tag in 2 in the applicationDidFinishLaunching method, detail view is displayed normally, but if i switch it back on 1, and then double click on the row, "master" view (with the NSTableView) remains visible, and nothing happen (views are not swapped)
Anyone can help me to find out what is wrong with my code?
Regards, John
You apparently had a second instance of your AppDelegate class instantiated in the MasterTableViewController.xib file. There should be only one AppDelegate instance and that's the one in MainMenu.xib. So, it shouldn't be in MasterTableViewController.xib.
One of the instances was receiving the double-click action method from the table, but the other one was the one with the outlet to the main window.
You need(ed) to get rid of the second instance and find another way to access the app delegate from the MasterTableViewController.

set UISearchBar Focused programmatically

i have 2 tabs on a UITabBarController, a navigation inside each tab, and on the first one a tableViewController with searchBar.
On the second tab, i have another searchBar, but, i want to set the onClick of this one, go to the first tab and focus the search bar.
i currently can redirect the tap on the searchbar to the other tab, but i dont know how to set the focus.
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
UINavigationController *tmp = [self.tabBarController.viewControllers objectAtIndex:0];
[tmp popToRootViewControllerAnimated:NO];
UIViewController *vc = tmp.topViewController;//THIS ONE
[self.tabBarController setSelectedIndex:0];
return NO;
}
The UIViewController that i've got on the code, has the UISearchBar object that i need, i mean, the superClass.h is:
#import <UIKit/UIKit.h>
#interface MasterTableViewController : UITableViewController <UISearchBarDelegate>
#property (nonatomic, strong) NSMutableArray *objects;//stuff
#property (nonatomic, strong) NSMutableArray *results;//stuff
#property (nonatomic, strong) IBOutlet UISearchBar *searchBar;//THIS IS MY UISearchBar !
#end
my problem is that i can't get that UISearchBar, how should i do it ?
and how can i say it to be focused from the searchBarShouldBeginEditing Method on the other tab ?
Modify your searchbar delegate method and check below:-
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;

Set label text from different from class file

What I would like the code to do is so when the button is pressed it runs the function in the Label.m file and it then sets the labels text to "test". Whenever I run it the code calls the function but doesn't change the labels text. Can someone please help me fix my code or show me the correct and easiest way to change a labels text from a class file.
In my FirstViewController.h
#interface FirstViewController : UIViewController{
IBOutlet UILabel *test;
}
#property (nonatomic, retain) IBOutlet UILabel *test;
In my FirstViewController.m
#import "Label.h"
-(IBAction)refresh:(id)sender {
[Label getSchedule];
}
In my Label.h
#import "FirstViewController.h"
#interface Label : NSObject
+ (void)getSchedule;
#end
In my Label.m
#import "FirstViewController.h"
#implementation Label
+ (void)getSchedule{
NSLog(#"log");
FirstViewController *VC = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
VC.test.text = #"test";
}
#end
Edit: As Maddy mentioned in the comments the original posters code would have worked if it was just called after the viewController had gotten all of its view objects. The easy way to achieve what the original poster wanted would be to simply add:
self.test.text = #"test";
to the viewControllers viewDidLoad method.
I'll leave my original answer here anyway, as I believe it improves on the original posters code and removes some of its dependencies. It still is way too complicated for what it wants to achieve but the pattern as such could be transferred to more befitting scenarios:
To elaborate on my comment:
Your method
+ (void)getSchedule{
NSLog(#"log");
FirstViewController *VC = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
VC.test.text = #"test";
}
Is a class method. So, naturally it is fired, but your UILabel instance test this instance have no idea about this. Furthermore you seem to have created your own class Label which subclasses NSObject, but the actual label instance is a regular UILabel.
I would guess what you are trying to do is something like this:
#interface Label : UILabel
- (void)getSchedule;
#end
...
#interface FirstViewController : UIViewController
#property (nonatomic, retain) IBOutlet Label *test;
Edit: forgot the method(!)
- (void)getSchedule{
self.text = #"test";
}
And finally in your viewController...
#import "Label.h"
-(IBAction)refresh:(id)sender {
[self.test getSchedule];
}

Setting the initial value of a UILABEL

I'm trying to create a simple Quiz app (I'm a beginner), when I launch the app I want a UILabel to show the first question (of an array of questions). I'm having some trouble with setting the initial value.
I've done a couple of attempts, whiteout success. I my QuizAppDelegate.h file I declare my UILabel like this:
IBOutlet UILabel * questionField;
In my main .m file I have tried the following:
- (id)init {
[super init];
questions = [[NSMutableArray alloc] init];
// Not working
questionField = [[UILabel alloc] init];
[questionField setText:#"Hello"];
// Working
NSLog(#"Hello");
[self defaultQuestions];
// [self showQuestion];
return self;
}
Another thing I have tried is the following in QuizAppDelegate:
#property (nonatomic, retain) IBOutlet UILabel *questionField;
- (void)changeTitle:(NSString *)toName;
And in the .m file:
#synthesize questionField;
- (id)init {
[super init];
questions = [[NSMutableArray alloc] init];
// Not working
[self changeTitle:#"Hello"];
// Working
NSLog(#"Hello");
[self defaultQuestions];
// [self showQuestion];
return self;
}
-(void)changeTitle:(NSString *)toName {
[questionField setText:toName];
}
Any tips on how to solve this would be great!
// Anders
Hopefully you're not actually putting code into main.m. On iOS, you rarely modify that file.
Since you're doing everything in the AppDelegate, let's keep it there (as opposed to creating a new UIViewController). Let's start with the basics.
Adding the Label as an instance variable
You're doing this correctly—inside the curly braces of the .h file, put the line
IBOutlet UILabel * questionField;
Then, declare the corresponding property, and make sure to synthesize it in the .m file.
#property (nonatomic, retain) IBOutlet UILabel *questionField;
#synthesize questionField // in the .m file
Adding the UILabel in Interface Builder
Open up MainWindow.xib. Drag a UILabel from the Library to the Window that represents your app's window. Then Control-Drag from the AppDelegate object (the third icon on the left in Xcode 4; it'll be labelled in the Document window in IB 3). You'll see a little black window come up—select the option called questionField to make the connection.
See this link for screenshots and how to make connections in IB. The same applies in Xcode 4.
Changing the text
You don't need a separate method to change the text—just modify the label's text property.
Pick a method that'll be called when the app launches (applicationDidFinishLaunching:WithOptions: is a good place to do it in), and put the following code:
questionField.text = #"Hello";
And that's it!
Code
QuizAppDelegate.h
#import <UIKit/UIKit.h>
#interface QuizAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UILabel *questionField;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UILabel *questionField;
#end
QuizAppDelegate.m
#import "QuizAppDelegate.h"
#implementation QuizAppDelegate
#synthesize window=_window;
#synthesize questionField;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:self.questionField];
[self.window makeKeyAndVisible];
self.questionField.text = #"Hello";
return YES;
}
- (void)dealloc
{
[_window release];
[questionField release];
[super dealloc];
}
#end
If you're creating the label programmatically, then you have to add the label to the view:
[self.view addSubview:questionField];
This assumes that you have a ViewController. If not, and you're doing this directly in the AppDelegate (a very bad idea, by the way), then do
[self.window addSubview:questionField];
If you're creating it in the IB, make sure you set up the connections.
You should not both add the UILabel in the IB and instantiate it programmatically. Only call alloc if you are creating it programmatically. Otherwise, if using the IB, skip that part. You created it already with the xib.
I suspect that you have either not created your Interface Builder layout properly - either you have missed the control out all together or more likely you have not connected that control to the questionField outlet in yout header file.
You need to drag a UILabel view into the main view and then connect it to the correct line in your header file.
You shouldn't be using your main.m like that at all. In fact, you should almost certainly never do anything with it. Try creating a UIViewController subclass and practicing your quiz with that. (Add the UILabel to the IB file and then connect the outlet.) Perhaps use the View-Based Application template while you are practicing.
This is a good answer:
"You're doing this correctly—inside the curly braces of the .h file, put the line
IBOutlet UILabel * questionField;"
I was trying to change the value of mylabel.text and the screen didn't update the label with this.value. I included the {IBOutlet UILabel * mylabel} and it works like a charm!
So this answer is valid to change the text of a label programmatically!
Thanks

specifying textField using UITextField delegate

Hi i'm new to software for iphone/ipad apps.
I am using the textField delegate method:
"(BOOL)textFieldShouldReturn:(UITextField *)textField".
I have gotten to the point where when the user enter a name and presses 'enter' and a label displays what the user just wrote. This is just two lines of code like so:
labelChange.text = textField.text; //labelChange is my label in IB
return YES;
Since i have several textfields in IB, this code works for all of them. I'm not sure how to be specific with the code and have this label change for only ONE of my textFields.
My .h files looks like this.
#interface FirstViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UILabel *labelChange;
IBOutlet UITextField *userName;
IBOutlet UITextField *homeValue;
IBOutlet UITextField *downPayment;
IBOutlet UITextField *textField;
}
#property (nonatomic,retain) UILabel *labelChange;
#property (nonatomic,retain) UITextField *userName;
#property (nonatomic,retain) UITextField *homeValue;
#property (nonatomic,retain) UITextField *downPayment;
#property (nonatomic,retain) UITextField *textField;
#end
I want my label to change only when user types in the text field labeled "userName".I'm not sure how to do this, am I missing something?In IB, I connected all my textfield delegates to 'Files Owner'. Any advice would be really helpful. thanks!
Assign each of your text fields a tag number i.e.
textField1.tag = 1
textField2.tag = 2
etc...
Then in your -(BOOL)textFieldShouldReturn:(UITextField *)textField you can do a:
switch (textField.tag) {
case 1:
labelChange1.text = textField.text;
break;
case 2:
labelChange2.text = textField.text;
break;
etc... etc...
}