how to set labels from another class - ios

I have problem with set text on labels. Labels are declarated in class1 but I want to use it and set it in class2 where I want to set data dependent on the selected row in tableView (is implemented in class2)
my declatation in class1:
#property (weak, nonatomic) IBOutlet UILabel *trackNameDetailsLabel;
my method in class2 (I try this but does not work):
-(void)setLabels {
self.trackNameDetails = self.result.trackName;
}
result.trackName is recall to data which are stored in url file
Thanks in advance.

You can give class 2 a reference of class 1 from itself by making a variable like Class1 *referenceOf1 = self and adding #class Class1; to the top of Class 2. Then, in class 2, you can make an instance of class 1 and calling that reference.
Alternatively, you can pass data through a singleton, which in my opinion is nicer and avoids memory leaks. Here's a link to learn more about singletons.
http://www.galloway.me.uk/tutorials/singleton-classes/

Looks like you have trackNameDetails for class 2, but you want to set value to trackNameDetailsLabel in class 1
So the code
-(void)setLabels {
self.trackNameDetails = self.result.trackName;
}
will only set the property trackNameDetails in class 2.
You need give a reference, which can be instance of class 1 to class 2 like Cole said.
In class 2,
#property(strong, nonatomic) Class1 * class1;
-(void)setLabels {
class1.trackNameDetails = self.result.trackName;
}
Remember to set class1 to a instance of Class1 before using this method

Related

Passing a UITextFieldDelegate to UIViewController helper class

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];

#property #synthesize equivalent in swift

I have something like
#property(nonatomic,retain) UIImageView *whiteBfFillUp;
#end
#synthesize locationManager;
I am new to swift coding. Can anyone tell me the equivalent code in swift.
There is no equivalent.
In Swift when you write a varor let in a class or struct declaration you already declaring a property.
Define properties to store values
This is what is written in Swift documentation.
If you are concerned about access control you can use private or public modifiers.
public var somePublicVariable = 0
If you'd like to override properties such as you did in Objective-C you will find useful properties observers such as didSet{} willSet{}.
If you need a a readonly properties you can make the setter private.
public private(set) var hours = 0
If you are only looking for equivalent of property, then you just need to create your class level variables. All class level variables are by default 'strong' or 'retain'. If, however, you want them to be weak then use weak.
This would be something like
var whiteBfFillUp: UIImageView? = nil
The ? at the end means that this is an optional type. If it's not, you would need to assign it some value in the init method, or right there.

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.

How to access protected variables in Objective-C

I want to have a variable that will only be visible to its subclasses which is exactly similar to what protected variables are in Java.
I tried like this on the parent's implementation file
#interface ParentClass (){
NSArray *_protectedVar
}
but unfortunately the protectedVar is not visible as soon as I call super.protectedVar
Correct my if I am wrong but I don't wanna use #properties to that variable since it will make that variable public.
And this is my subclass's header file looks like #interface SubClass : ParentClass
The code you posted declares an instance variable in an Objective-C class extension, and therefore the variable's default visibility is private. You can use a visibility modifier to change the ivar's visibility, as shown below:
#interface ParentClass ()
{
#protected
NSArray *_protectedVar
}
If you have this
ParentClass.h
#interface ParentClass : NSObject{
NSArray *_protectedVar
}
Then just like how you access normal ivar, use _protectedVar directly.
But I suggest you use property with private header
Parent.h
#interface Parent : NSObject
#property id publicProperty;
#end
Parent_Protected.h
#interface Parent (Protected)
#property id protectedProperty
#end
So normal class only #import "Parent.h", they can't see protected property.
But subclass can #import "Parent_Protected.h" and use protected property with self.protectedProperty

When to declare something in category .m file or in header .h file? [duplicate]

This question already has answers here:
Why is there another #interface inside the.m file? [duplicate]
(6 answers)
Closed 9 years ago.
As we know, normally we used to declare our class instance variables, properties, method declarations in class header file (.h).
But we can do the same things, in .m file, using blank category.
So my question is: what should be declared in .h file and what should be declared in .m file - and why?
Regards,
Mrunal
New Edit:
Hi all,
If you refer to newly added Apple examples over developer.apple.com - they are now declaring their IBOutlets and IBActions in .m file itself and that too with property declaration. But we can achieve the same thing by declaring those references in .h file in class private member section.
Then why are they declaring those in .m file and as properties, any idea?
-Mrunal
But we can do the same things, in .m file, using blank category.
A class continuation.
Normally, you choose to declare something in the header if it is intended to be public -- used by any client. Everything else (your internals) should typically go in the class continuation.
I favor encapsulation -- Here's my approach:
variables
Belongs in the class continuation or #implementation. Exceptions are very, very rare.
properties
Typically belongs in the class continuation in practice. If you want to give subclasses the ability to override these or to make these part of the public interface, then you could declare them in the class declaration (the header file).
method declarations
More in the class continuation than in the class declaration. Again, if it is meant to be used by any client it would belong in the class declaration. Often, you won't even need a declaration in the class continuation (or class declaration) -- the definition alone is adequate if it is private.
Basically, in the header file (.h) you declare your public API, while in the implementation file (.m) you declare your private API.
Visibility in Objective-C
You can also find the answer here
It's mostly up to you.
The .h file is like the description of your class.
It's smart to only put in the .h file what's really important to be visible from the outside of the class, especially if you're working with other developers.
It will help them to understand more easily what methods/properties/variables they can use, rather than having a whole list of things they don't.
Usually you want to use blank category in .m file for declaration of private properties.
// APXCustomButton.m file
#interface APXCustomButton ()
#property (nonatomic, strong) UIColor *stateBackgroundColor;
#end
// Use the property in implementation (the same .m file)
#implementation APXCustomButton
- (void)setStyle:(APXButtonStyle)aStyle
{
UIColor *theStyleColor = ...;
self.stateBackgroundColor = theStyleColor;
}
#end
If you try to access property declared in black category outside .m file, you will receive undeclared property compiler error:
- (void)createButton
{
APXCustomButton *theCustomButton = [[APXCustomButton alloc] init];
theCustomButton.stateBackgroundColor = [UIColor greenColor]; // undeclared property error
}
In most cases, if you want add new method/properties to an existing class without subclassing, then you want declare category in .h file and implementation of declared methods in .m file
// APXSafeArray.h file
#interface NSArray (APXSafeArray)
- (id)com_APX_objectAtIndex:(NSInteger)anIndex;
#end
// APXSafeArray.m file
#implementation NSArray
- (id)com_APX_objectAtIndex:(NSInteger)anIndex
{
id theResultObject = nil;
if ((anIndex >= 0) && (anIndex < [self count]))
{
theResultObject = [self objectAtIndex:anIndex];
}
return theResultObject;
}
#end
Now you can use "com_APX_objectAtIndex:" method wherever "APXSafeArray.h" is imported.
#import "APXSafeArray.h"
...
#property (nonatomic, strong) APXSafeArray *entities;
- (void)didRequestEntityAtIndex:(NSInteger)anIndex
{
APXEntity *theREquestedEntity = [self.entities com_APX_objectAtIndex:anIndex];
...
}

Resources