UITextFieldDelegate SecondView - objective-c

I'm using iOS 5 and trying to use TextField with UITextFieldDelegate, it's worked exactly like I want (but JUST in the first View). I don't understand, why it's not working in the next view.
For simple example, I created new project (ViewController). There I added one button, that connect to another view (SecondViewController). In the SecondViewController, I have one TextField. With this textField I want to use textFieldShouldReturn. But it seems, that this method is not being called. What I know, I should write the delegate in ViewDidLoad. Should I write myTextField.delegate = self; ? but I think something wrong there. I used Debugging, and always at that position, i'm getting problem. Could you please tell me, what the problem is? and how can i solve it ;(
Thanks in advance.....
Here is my code (that it works, in the first view (ViewController). Unfortunately here not (SecondViewController):
SecondViewController.h
#interface SecondViewController : UIViewController<UITextFieldDelegate>
#property (strong, nonatomic) IBOutlet UITextField *myTextField;
#end
SecondViewController.m
#implementation SecondViewController
#synthesize myTextField;
- (void)viewDidLoad{
[super viewDidLoad];
myTextField.delegate = self; // here i get the problem
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{ // this method is not being called
[textField resignFirstResponder];
NSLog(#"is called!");
NSString *valueMyTextField = myTextField.text;
NSLog(#"%#", valueMyTextField);
return YES;
}

Problem solved... :)
The problem was the connection from firstView to secondView.
Do not use addSubView, if you want to add Controller!
Use presentModalViewController :)
Hope it helps, in case you have the same problem like me.

In the nib file, please check that whether you have checked the Auto-enable Return Key check box for the text field.

Related

Data encapsulation, Instance variable cannot be accessed

I'm having some trouble understanding what classes can read what variables in other classes. I've read to many different things online and cant seem to find anything solid in here. I've literally wasted the past two days trying to get my program to work but no classes can read any other classes variables. Any help will be GREATLY appreciated.
This is my ViewController.h:
#interface ViewController : UIViewController
{
#public
NSString *nameOfLabel;
}
#property (strong, nonatomic) IBOutlet UILabel *firstLabel;
- (IBAction)Switch:(id)sender;
- (IBAction)changeLabel:(UIButton *)sender;
-(NSString *) nameOfLabel;
#end
nameOfLabel is a public variable and should be able to be accessed by an outside class, right?
ViewController.m:
#import "ViewController.h"
#import "NewView.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)Switch:(id)sender {
NewView * new = [[NewView alloc] initWithNibName:nil bundle:nil];
[self presentViewController: new animated:YES completion:NULL];
}
- (IBAction)changeLabel:(UIButton *)sender {
nameOfLabel = #"Test Name";
_firstLabel.text = nameOfLabel;
}
-(NSString *) nameOfLabel {
return nameOfLabel;
}
#end
changeLabel button changes *firstLabel.text to "Test name".
second class is NewView.h:
#import "ViewController.h"
#interface NewView : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *secondLabel;
- (IBAction)changeSecondLabel:(UIButton *)sender;
#end
and NewView.m:
#import "NewView.h"
#interface NewView ()
#end
#implementation NewView
{
ViewController *view;
}
- (IBAction)changeSecondLabel:(UIButton *)sender {
view = [[ViewController alloc] init];
_secondLabel.text = view.nameOfLabel;
}
#end
changeSecondLabel should change secondLabel.text to nameOfLabel which is 'Test name', however, the label actually disappears which makes me think that nameOfLabel cannot be reached. Ive played around with nameOfLabel, making it a #property and then synthesising it, as well as trying putting it in { NSString *nameOfLabel; } under #implementation but I still get the same result.
This line: view = [[ViewController alloc] init]; creates a new ViewController which doesn't know anything about what you may have done to some other ViewController. In your case, it specifically doesn't know that changeLabel: was called on another ViewController before this new one ever existed.
When the second view controller (NewView) is presented, it has no reference to the first view controller (ViewController) and it's data.
Here are a couple of suggestions.
In modern Objective-C I'd recommend using properties instead of exposing a variable.
Look over the naming in general. "ViewController" is not a good name for example.
If the property is part of an internal state of the class, declare it in a class extension.
Before you present the second view controller, set a reference to the string from the first view controller.
Part of ViewController.m:
#interface ViewController ()
#property (copy,nonatomic) NSString *nameOfLabel;
#end
#implementation ViewController
- (IBAction)Switch:(id)sender {
NewView *new = [[NewView alloc] initWithNibName:nil bundle:nil];
new.secondLabel.text = self.nameOfLabel;
[self presentViewController: new animated:YES completion:NULL];
}
First of all please read about coding standards, it's not a good practice to:
Name variables like "new"
Name methods like "Switch"
Name UIViewController like "view" or "NewView"
Regarding logic:
This is all messed up here. What you actually do is you create viewController with nameOfLabel which is empty and is only changed on button press. I assume you press that button so it's changed. Then on switch action you create another viewController and present it. Then from inside that new viewController you create another new viewController which has empty nameOfLabel, get this empty value and put it inside secondLabel.
There are couple of ways you can do to change secondLabel:
Move nameOfLabel to model and read it from there when you want to change secondLabel,
Because your new viewController is child of viewController that keeps nameOfLabel you can access it by calling [[self presentingViewController] nameOfLabel] but make it property first,
Pass nameOfLabel through designated initializer.
Well, if you want a simple demonstration of access of a public ivar, the syntax is:
view->nameOfLabel;
^^
not dot-syntax:
view.nameOfLabel;
(dot-syntax just goes through accessor methods).
I've only seen a handful of warranted edge cases over the years; there's rarely, rarely ever a good reason to make an ivar public (also, protected is also rarely a good choice).

UITextField not calling delegate method

I have a DetailViewController, which implementation file contains this code:
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController <UITextFieldDelegate>
{
__weak IBOutlet UITextField *nameField;
__weak IBOutlet UITextField *numberField;
}
#end
In my storyboard, I have set the ViewController's to DetailViewController and connected the delegate of both of my UITextFields to my DetailViewController. The implementation file of my DetailViewController contains this method to dismiss the keyboard when tapping somewhere other than the text field:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
This method is not called though, I have tested this using a breakpoint. What could be going wrong?
rdelmar is correct, the code you have only gets triggered when the user hits the "return" key on the keyboard, not when they click outside of the keyboard.
To get the behavior you are looking for, I'd add a Tap Gesture Recognizer to the view behind your text field, then put [nameField resignFirstResponder]; and [numberField resignFirstResponder]; in the tap gesture recognizer's code.
I created a sample project
added to the viewcontroller
added a textfield and connected its delegate
write the code in viewcontroller
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
it worked fine.Tried weak it also worked both as instance variable and property
So check if that you are reallocating it anywhere and also check its memory is same there using breakpoint
Why do you use _weak? Remove it.
In your .m file do (in viewDidLoad):
[nameField setDelegate:self];
Same for the other textfield(s).

editButtonItem Not Showing Up

The Problem:
The built-in editButtonItem that Xcode automatically comments out when a new UITableViewController class is created does not work when I delete the comment slashes (//). By does not work I mean that the edit button does not appear at all. Swiping a cell does not work either.
Attempted Solutions:
I have tried to follow the various workarounds that have been posted on other stackoverflow threads to no avail. Most of the posts that I have found talk about various aspects of the edit button not working (e.g., no minus signs showing up, etc…) but very few that I have found in which the edit button does not show up at all.
Hunch:
I have a hunch that it might have something to do with the UITableViewController not being properly implemented. I am very new to object-oriented programming as well as objective-c, so I apologize if the answer is something very basic—but hey, it's part of the learning process. Any help is much appreciated.
Code:
____.h
#import <UIKit/UIKit.h>
#import "IndividualRecipeViewController.h"
#class BrowsePrivateRecipeViewController;
#protocol BrowsePrivateRecipeViewControllerDelegate
- (void)browsePrivateRecipeViewControllerDidFinish:(BrowsePrivateRecipeViewController *)controller;
#end
#interface BrowsePrivateRecipeViewController : UITableViewController
#property (weak, nonatomic) id <BrowsePrivateRecipeViewControllerDelegate> delegate;
#property (assign, nonatomic) NSUInteger listLength;
#property (strong, nonatomic) NSDictionary *dictionaryOfRecipes;
#property (strong, nonatomic) NSMutableArray *arrayOfRecipeNames;
// ... methods
#end
____.m
#interface BrowsePrivateRecipeViewController ()
#end
#implementation BrowsePrivateRecipeViewController
#synthesize delegate = _delegate;
#synthesize listLength = _listLength;
#synthesize dictionaryOfRecipes = _dictionaryOfRecipes;
#synthesize arrayOfRecipeNames = _arrayOfRecipeNames;
- (void)viewDidLoad
{
// ... code here
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// ... other methods
UPDATE:
LINK TO SOURCE CODE
So I have decided to post the source code to my whole project. I am having this problem with multiple files, but if I get it fixed in one, I am pretty sure that the rest will fall into place.
Please focus on the files BrowsePrivateRecipeViewController.m/.h. This is the most straightforward instance of the problem.
Once again thank you for your patience and help.
Sincerely,
Jason
First of all, I would definately not use a custom button for editing the table. It's unnecessary simply because there's already one built in.
Just use UIViewControllers editButtonItem.
If you have to perform additional stuff on button press, override -setEditing:animated: and call super first.
The error you mentioned above is caused because you're trying to access the navigationBars navigationItem, which does not exist. You should access your view controller's navigationItem.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
You need to make a button first. This will make an Edit button then add it to the rightBarButtonItem spot.
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
self.navigationItem.rightBarButtonItem = editButton;
You then need to set up a method to turn on the table's edit mode.
- (void)editTable
{
[self.tableView setEditing:YES animated:YES];
}
Update:
Just read your question again and noticed you want swipe to delete as well. You need to added these methods in order to add that to your tableview.
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Update 2
__.h
#property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar;
__.m
#synthesize navigationBar;
- (void)viewDidLoad {
//...
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
self.navigationBar.navigationItem.rightBarButtonItem = editButton;
}
You aren't alloc'ing or init'ing the editButtonItem object, so how can you expect to retain it (equals sign), let alone have it show up? You're basically sending a message to nil.

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

Prompting and Delegate question

I have a class that needs to ask the user a question and wait for the users response to determine the next action. What would be the best way to do this? Using a delegate? How? I have a UITextField and a UITextField in the class.
Thanks
It all depends upon how you wish for the user to submit the data. The most user friendly way is to do as TahoeWolverine explained and implement - (BOOL)textFieldShouldReturn:(UITextField *)textField from UITextFieldDelegate. In order to use this, the class that implements textFieldShouldReturn: must have <UITextFieldDelegate> protocol in its interface declaration; moreover, the textfield in question must have the UITextFieldDelegate-implementing class set as its delegate. In most cases those would look like this:
#interface SomeViewController : UIViewController <UITextFieldDelegate> {
UITextField *myField;
}
#property (nonatomic, retain) IBOutlet UITextField *myfield
#end
and somewhere in the implementation:
[[self myField] setDelegate:self];
Finally, implementing the UITextFieldDelegate protocol:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == [self myField]) {
[self doSomethingWithText:[[self myField] text]];
}
}
Hope that helps.
Yes, you should use a delegate, and link that to the keyboard's done button (I'm assuming that you're presenting the user a keyboard). Simply link your delegate to the return key of the keyboard, and that should do the trick.