Passing a UITextFieldDelegate to UIViewController helper class - ios

I have a helper class which looks something like this:
(We'll call this GUI.H & GUI.M Files)
(DOT . H FILE )
- (CGGroup)addLabelTextField:(UIViewController*)vc deligate:(id<UITextFieldDelegate>)deligate textField:(UITextField*)textField;
(DOT . M FILE )
- (CGGroup)addLabelTextField:(UIViewController*)vc deligate:(id<UITextFieldDelegate>)deligate textField:(UITextField*)textField
{
// Blah, Blah Blah Blah Set Up & Configure Label & Textfield... 10 lines
// DONE Allocating & Configuring OK
// Last 3 Lines -- this is where the Deligate gets SET and the problems
textField.delegate = deligate; // <- this is what is NOT happening
[vc.self.view addSubview:textField]; // this adds TextField to passed in UIViewController
return(cgTmp); // nothing to see here just a typedef of CGRect's
}
I pass a pointer of the ViewController, textField, and textFieldDeligate in and set them programmically (NO xib's what-so-ever)
Alloc, set, ect, is just like you would create / configure a textfield or label in the viewcontroller you were going to use it, except pass that the the helper class sets everything. so adding fields is one call, not all the usual configuring...
Problem:
Everything seems to be working fine, with the except that the TextFieldDeligate functions are NOT getting called (clang -- no warnings, nothing...)
If I move this same code, back into the calling class (uiViewController)
Suddenly, the delegates are getting called, no issues at all...
Another issue: once the testfield is set, you are not able to change things, I.E... SecureText = TRUE to FALSE.... Nothing happens, again when this code is moved back into the calling viewController, Suddenly everything works as advertised....
I suspect something to do with the way the "UITextFieldDeligate" is being passed, but as I mentioned, clang doesn't give ANY warnings... NONE...
The UIViewController is of course set up to support the deligates
and the properties are declared...
(if I move this stuff back into the class (the UIViewController) (without having a separate class for these setting these fields) it works fine...
The method gets called:
SomeUIViewController.M (This is where we call the function defined in GUI.H & M)
[self.gui addTextField:self textField:self.passField deligate:self.passField.delegate];
AND YES There is a Property for the UITextField and the Class has the protocol declared
SomeUIViewController.H
#interface SomeUIViewController : UIViewController <UITextFieldDelegate>{
}
#property (nonatomic, strong) GuiUtils *gui;
#property (nonatomic, strong) UITextField *userField;
#property (nonatomic, strong) UITextField *passField;
#property (nonatomic, strong) UITextField *tmpField;
And as I mentioned b4, the only plroblem I'm experiencing is the deligate methods in the UIViewController (related to this UITextField) are NOT getting called...
(But I think it may be something simple, because I just noticed I'm not passing the deligate as a pointer... So it may have already been solved)
Thanks in advance.....

OK.... The Delegate NOT getting called is FIXED!
I was passing the delegate of the "UITextField.delegate" instead of the delegate of the UIViewController "self"
FROM:
[self.gui addTextField:self textField:self.passField deligate:self.passField.delegate];
TO:
[self.gui addTextField:self textField:self.passField deligate:self];

Related

Access int from Another Objective-C Class

I've trying to get an int from another class and it just says that the number is 0 (but it's not).
This is in Class2.h:
#interface Class2 : SKScene <SKPhysicsContactDelegate>{
Class1 * class;
}
This is in Class2.m:
NSLog(#"%i", class.thisint);
This is in Class1.h:
#property (assign, nonatomic) int thisint;
This is in Class1.m (in viewDidLoad):
thisint = 5;
The NSLog is being called well after the viewDidLoad method but it just keeps saying 0. How do I get this int from Class1? I don't know if the fact that Class2 is an SKScene affects this...
class is an existing method on NSObject and I would hope that you've just used that name for the purposes of this question - if not, please change the name of the variable, it will only lead to confusion.
What do you see if you log class:
NSLog(#"%#",class);
(Put that next to where your existing log is)
How and where are you assigning to the class variable?
A value of 0 probably means one of two things:
class is nil. This means you haven't assigned it anywhere. You don't magically get a value in a property just because you've declared one
class is a different instance to the one you think it is. This is a common beginner mistake, where you've done something like
class = [[Class2 alloc] init];
Which creates a new instance. You need to get a reference to the existing instance, which I can't tell you how to do without seeing more code.

TabBarController: accessing a property of inside UIViewController

I am working on an app with three tabs.
From first tab, I want to access variables which happen to be defined as #property inside tab # 3 [- defined by SettingsViewController].
I added the below import line to the header file of the first tab:
#import "SettingsViewController.h"
My SettingsViewController header has IBOutlet as below:
#property (retain, nonatomic) IBOutlet UITextField *username;
From implementation inside the first tab I call:
NSLog(#"Figuring out how to access the Settings view %#", self.tabBarController.viewControllers[2]);
It correctly points to the right object, printing out:
2014-08-11 14:46:12.700 backup[30043:60b] Figuring out how to access the Settings view <SettingsViewController: 0x8db2020>
The below however does not work:
self.username = (SettingsViewController *)self.tabBarController.viewControllers[2].username;
I get an XCode static code analyzer error: property 'username' not found on object of type 'id'
What is the proper Objective-C way of casting id type to the what I need?
You just need to fix your parenthesis. Currently it is evaluating "self.tabBarController.viewControllers[2].username" and then doing the cast
This should work:
self.username = ((SettingsViewController *)self.tabBarController.viewControllers[2]).username;

Property declaration craziness iOS 7

I am at my wits end with a property declaration in a iOS class. In my .h file I have the following declaration :
#property (strong, nonatomic)NSString *sessionID;
In my .m file I have this code :
- (void)setSessionID:(NSString *)aSessionID
{
_sessionID = aSessionID;
// Custom code to set this in a global context
}
This is all fine and compiles with no issues. Now I need to have the sessionID return a default value if nothing is set, however the moment I add this line :
- (NSString *)sessionID
{
return _sessionID ? _sessionID : #"defaultSession";
}
then the first line in the setSessionID:
_sessionID = aSessionID;
causes an error with "Use of undeclared function _sessionID. Did you mean aSessionID", I am at my wits end to figure out what is causing it.. I have so many classes with variables and have never seen this before... what is causing this? I restarted Xcode, cleaned out the project and no luck.. If I remove the - (NSString *)sessionID method, then it stops complaining.. but the moment I add the method declaration the Xcode marks it as an error.
Anypointers accepted! :)
Edit: I also noticed, that in this class if I add any property accessor method it complains about the ivar.. e.g. I have another property declared
#property (strong, nonatomic) NSString *userEmail
The moment I add -(NSString *)userEmail, the ivar _userEmail usage above it all becomes undeclared.. :(
If you override both the setter and getter of a property, the compiler will not automatically synthesize the backing ivar for you. You need to do a manual synthesis,
#synthesize sessionID = _sessionID;

property not found on object Type (custom) xcode

the strangest thing happened. Although I don't think I touched anything in that class, suddenly it started telling me it couldn't find an array in a class...
Here are the errors:
basically it cannot access the mutable array in baseobject (custom Car.h type)
(semantic issue: property objectReadyForCoreDatabase not found in object of type CarPacket (false, because it is declared))
if([baseObject.objectsReadyForCoreDataBaseInput count]<kLenght )
{
}
car packet .h
#import <Foundation/Foundation.h>
#import "ResponsePacket.h"
#interface CarPacket : ResponsePacket
#property (nonatomic, copy) NSString *objectID;
#property (nonatomic, retain) NSMutableArray *objectsReadyForCoreDataBaseInput;
#property (nonatomic, assign) NSInteger timeStamp;
#end
It is weird because on the same page where I get the error if I type object.objectID it recognizes that but not object.objectReadyForCoreDataBaseInput (also it just suddenly stopped working)
Please let me know if you have any ideas... Thank you
I tried restoring to previous snapshots and it had no effect... it still showed the error (even though I know on that date it didn't)
You haven't shared much about the context of where you're making the call (and seeing the error). That said, my guess would be one of two things: The calling class isn't familiar with the receiving class (CarPacket), or, the calling class doesn't know that baseObject is a CarPacket.
Where are you calling from? Make sure the calling class imports the headers. Since I don't know where you're calling from, let's say it's from within UnknownClass:
UnknownClass.m
#import UnknownClass.h
#import CarPacket.h // This should make your class familiar
#implementation UnknownClass
The other thing is that you need to make sure that at the time you're touching the baseObject, your UnknownClass instance knows that it is dealing with a CarPacket instance, e.g.:
- (void)someMethodOfUnknownClass
{
CarPacket *baseObject = (CarPacket *)baseObject; // Cast baseObject if it hasn't been declared as a CarPack in scope...
if([baseObject.objectsReadyForCoreDataBaseInput count]<kLenght )
{
}
}

errors when trying to get the value of a variable in another view

please i got errors when i try to read the content of a variable in view2 which was initialized in view1, i explain :
view1 is named RechercherViewController
view2 is named StationsSurLaCarteViewController
RechercherViewController.h :
#property (nonatomic,copy) NSString *typeCarburantChoisi;
RechercherViewController.m :
#synthesize typeCarburantChoisi;
StationsSurLaCarteViewController.h
#import "RechercherViewController.h"
#interface StationsSurLaCarteViewController : UIViewController {
IBOutlet AideStationsSurLaCarteViewController *aideStationsSurLaCarteViewController;
IBOutlet UITextField *textField;
}
#end
StationsSurLaCarteViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
textField.text=RechercherViewController.typeCarburantChoisi;
}
when building the app, i got actually two errors :
error: expected specifier-qualifier-list before 'StationsSurLaCarteViewController'
and
error: accessing unknown 'typeCarburantChoisi' class method
thx for help :)
First of all you have defined an instance property typeCarburantChoisi but in your StationsSurLaCarteViewController.m code you are trying to access kind of a class property (btw, there is no such thing in Objective-C). You will instead need a reference to your RechercherViewController instance and ask it for the property – this will resolve the second compiler error.
Regarding the first error I am not really sure what happened here. Maybe you have an error in your RechercherViewController.h file?
In any case, you should rather not import the interface file into StationsSurLaCarteViewController.h. Instead, use
#class RechercherViewController;
and import the full declaration in your implementation file StationsSurLaCarteViewController.m only.
Also, did you mix up AideStationsSurLaCarteViewController and RechercherViewController in your example?

Resources