reference to UITextField not working? - iphone

I have created a controller class in which I have created a outlet to UITextfield. But The problem is that I am unable to get reference in the controller class, so unable to call any of textfield methods. The reference is returning null.
I have defined outlet (and connected them) properly to the text field in class but still not able to find the error.I am clean bowled !!

<UITextFieldDelegate>
Did you added this delegate into .h file of your ViewController? Which is required to used its methods?

Have You Adopted
"UITextFieldDelegate" Protocol in your "ViewController.h"..?.
If Not You need to Add that protocol inside the "< >" of your "ViewController.h".
UITextFieldDelegate protocol contains various Method Declaration which called during the Various Event(begin text Editing,ended text Editing,).
For detail Information go Through this Link.

Related

Change UITextField.text from an external class swift

I'm trying to simplify my life by dividing chunk of related code into different classes.
But I encounter a problem: changing the ViewController property (like UITextField.text, etc.) from those classes.
I have tried creating protocol, but that was really messy so I want to find a new way to do this.
So, is there a simple way to change ViewController property directly from external classes? I'm using Swift 3 btw.
Thank y'all for helping.
If the object from where you want to change your label is a child object owned or presented by the controller, than you need to use delegation to comunicate back with the controller. You declare a protocol in that object from where you want to change the controller's label, and declare a weak property of that protocol type called delegate. When the controller creates that object you will set that object's delegate property to the controller itself (If that object is ownee by the controller, declaring the property weak you will not create a retain cycle). In the controller you implement the method declared in the protocol and in the implementation you can change the label.
The other case when the controller is owned or presented by the object from where you wanna change the label, in yhis case you just need a public method to do that.

UIView Subclass not working

I have an XCode project I am working on that has multiple views controlled by a tabBar that is throwing multiple instances of the error:
"cannot find interface declaration for '~my sub view name~', superclass of '~my sub view name~'
These sub views were created after the original project as new files. Each has a line of code like this:
#interface meViewAndEdit : meViewAndEdit
I see no #import statements either. The project fails to build and I am not sure what i should be setting these to. Should I be referencing my App delegate as the superclass? what is missing here?
UPDATE: (updated)
I changed the interface statements for each of my header files for the views i created as follows
#interface friends : UIView
BUT, it seems that i have a new issue that i'll have to research:
"UIView" may not response to "initWithNibName.bundle"
This is now present in each of the .m files for the views I created.
still learning, thanks in advance for your input.
When declaring a class in Objective-C, you need to specify the class name, as well as the superclass. In your code #interface meViewAndEdit : meViewAndEdit, you are essentially declaring a class that is a subclass of itself. Since you are trying to create a subclass of UIView, the class declaration should look as follows: #interface meViewAndEdit : UIView.
Also, in a comment in another answer, you asked whether or not you need to #import the app delegate in every class. The answer to this is usually no, unless you specifically need to access something involving your specific app delegate class.
I will also make note that it is conventional to give classes a capitalized name. For example, it should be MeViewAndEdit, rather than meViewAndEdit. You only should keep the first letter lowercase if it is the name of a variable or function.
EDIT: The reason that you are encountering the errors in your update is that you are trying to subclass UIViewController, not UIView. On top of this, instead of subclassing either one, you are subclassing the app delegate. Change your line of code #interface subviewname : my_app_delegate to #interface subviewname : UIViewController. You are trying to create a subclass of UIViewController, not my_app_delegate.
On another conventional note, it is never good to put underscores in a class name. Always name classes in camel case like MyAppDelegate, not my_app_delegate.
You need to #include the .h header files in all classes in which you are using them.

Can't get the methods at interface builder

I declared one method in sample.h and implemented in sample.m . Now i want to perform one action through connecting to a button and that method. but the files owner doesn't show any of my methods. Whats the problem.. Can anyone help me. i am new to this.
Thanks in Advance.
Have you specified that the File's Owner is of type sample? You can do that on the Identity tab in Interface Builder.
If sample is not a subclass of UIViewController you would most likely add an NSObject in Interface Builder and then set the Type to sample. You would also most likely have to add an IBOutlet to your view controller/File's Owner of type sample and connect it to the sample object in IB.
You want to make a function called on button press event.
so declare method in .h file as
-(IBAction) click:(id)sender
then it shows in file's owner inspector.Make connection with button for touchUpInside.

Setting UITableViewController Delegate?

If I have a class of type UITableViewController and I am going to add the required delegate methods to that class am I right in thinking I don't need to specify a delegate as the class defaults to using itself?
I am not quite sure it is a good idea to make a class be its own delegate. Maybe it would work with UITableViewController, but I read that this wouldn't work with UITextField for example, because it intoduces a message loop, see: Problem with TextField set as its own delegate
However, whether you decide on trying or specifying another delegate, you need to adopt the UITableViewControllerDelegate protocol in any case.

Arbitrary objects to handle actions?

My question may be a bit stupid, but why can't any object instantiated in IB handle, say, button click? I mean I'm able to add my own object to a xib, and link outlets to cotrols and control actions to object's method, but once I press the button everything just crashes (uknown selector).
Do you guys have a hint around that?
EDIT: The code, as requested:
#interface TextController {
IBOutlet UILabel * textLabel;
IBOutlet UITextField * textField;
}
-(IBAction)buttonClicked:(id)sender;
#end
#implementation TextController
-(IBAction)buttonClicked:(id)sender {
textLabel.text = #"Ololo";
}
#end
Connections in IB are ok, just believe me. It's really hard to get them wrong with all this drag'n'drop stuff :)
EDIT 2: TextController is not a file owner (in this case it works fine). However, I just want to understand why I can't wire up an action to some object (may be even not a subclass of UIViewController).
You can wire outlets and actions to any object in the nib-file. Drag an NSObject form the library palette onto your nib-file, in Interface Builder. Then go to the Identity tab of the information palette and set the Class of your object.
This way you can instantiate any object of any class from your nib. If the target you want to hook to is statically created from the nib-file. Make sure that the file's owner have at least one reference to your object, or else it will be deallocated as soon as it has been created. Targets are not retained by the sender.
If the object you want to hook up should not be statically created from your nib, then implement awakeFromNib in a class that is instantiated from the nib-file and hook up the targets in code.
Last option is if you do not have any sub-class of your own in the nib-file at all. Then implement initWithNibName:bundle: in your UIViewController subclass, and hook up your targets in code after calling the super implementation.
post code, this usually means you dont have your connections wired up correctly. Is file's owner TextController in IB?