call function which changes an IBoutlet - objective-c

Im calling a function in Class B which is in Class A:
ClassA *object = [[ClassA alloc] init];
[object changemethod];
[object release];
In Class A:
-(void)changemethod{
[imageView setImage: [UIImage imageNamed:#"picture.png"]];
NSLog(#"Works");
}
Calling the function in Class A works fine (I get the Logmessage) but the "imageView" is not changing the image. Why? Any clue?

Ok found a solution for my problem. I changed the "changemethod" to a class method. And to access the IBImageView in the class method I put it before the "#implementation". The IBImageView gets added in the "viewDidLoad" method and then changed by calling the classmethod "changemethod". Thanks.

Related

How to get Class object without instance?

Is there any way to get the result of:
NSObject *a = [[NSObject alloc] init];
[a class];
Without instantiating?
I wish to pass Class object to function as argument, and check it then. Like this:
- (void) pushControllerOfClass: (Class) aClass
{
if([aClass isSubclassOfClass: *what should I write here?*]) {
...
}
}
Intuitively, I've tried to write
if([aClass isSubclassOfClass: UIViewController]) {
But it doesn't work.
Thx for future response.
Update:
Is such a function considered bad in ObjectiveC?
I've refactored code from Nahavandipoor's book iOS 5 Programming Cookbook.
It was like that:
- (void) pushSecondController{
SecondViewController *secondController = [[SecondViewController alloc]
initWithNibName:nil
bundle:NULL];
[self.navigationController pushViewController:secondController animated:YES];
}
As for me, this is kind of bad function:
It doesn't parametrized when it should be.
Try with:
if([aClass isSubclassOfClass:[UIViewController class]])
In answer to your question, you don't need an instance of an object to get its class. You can get it directly from the class like:
[NSString class];
[YourClass class];
However, if all you are wanting is a method that can push any view controller you give it, then you can just use UIViewController as the parameter type:
- (void)pushAnyViewController:(UIViewController *)viewController
{
[self.navigationController pushViewController:viewController animated:YES];
}
This will take any view controller that is a subclass of UIViewController and push it onto the navigation stack.
If you really want the method to handle the allocations as well you can do:
- (void)pushViewControllerClass:(Class)viewControllerClass
{
if ([viewControllerClass isSubclassOfClass:[UIViewController class]])
{
id viewController = [[viewControllerClass alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
}
}
And call it like this:
Class viewControllerClass = [MyViewController class];
[self pushViewControllerClass:viewControllerClass];
Just make sure that the nib is named the same as the class if you are using one.
You can write [UIViewController class] to get the class object.
There should be a class method on most classes to get the class.
If you find yourself doing this often, perhaps revisit your design?

Subclassing Objective C Class methods

I have a question regarding Subclassing and Class methods.
I have a base class MyBaseClass which has a convenience class method
+ (id)giveMeAClassUsing:(NSString *)someParameter;
MyBaseClass is not a singleton.
Now, I wish to create a subclass of MyBaseClass, let's call it MyChildClass. I wish to have the same class method on MyChildClass as well. Additionally, I also wish to initialize an instance variable on MyChildClass when I do that.
Would doing something like this:
+ (id)giveMeAClassUsing:(NSString *)someParameter {
MyChildClass *anInstance = [super giveMeAClassUsing:someParameter];
anInstance.instanceVariable = [[UIImageView alloc] initWithFrame:someFrame];
return anInstance;
}
be valid?
Thanks for all your help (in advance) and for resolving my confusion and clarifying some concepts!
Cheers!
That will work fine.
Possibly better would be to define your convenience constructor in such a way that you don't need to override it:
+ (id)myClassWithString: (NSString *)string {
return [[[self alloc] initWithString:string] autorelease];
}
This will do the right thing no matter which of your superclass or any of its subclasses it is called in.
Then change just the initWithString: method in your subclass to handle the initialization:
- (id)initWithString: (NSString *)string {
return [self initWithString:string andImageView:[[[UIImageView alloc] initWithFrame:someFrame] autorelease]] ;
}
Absolutely that's valid.
One note though is in the superclass, reference the class itself with self, rather than the superclass by name.
This is bad:
// MySuperClass // BAD :(
+ (id)giveMeAClassUsing:(NSString *)someParameter {
return [[[MySuperClass alloc] initWithParam:someParameter] autorelease];
}
But this is good!
// MySuperClass // GOOD! :D
+ (id)giveMeAClassUsing:(NSString *)someParameter {
return [[[self alloc] initWithParam:someParameter] autorelease];
}
Otherwise when you subclass, and then call super you aren't actually initializing the right class. Use of self allows the class being instantiated to vary without overriding the class method.

obj-c, confusion why can't I add a setter to this class?

Normally I don't have any problem adding a setter method to a class.
However, I'm trying to add to library usage class, which must be causing the problem.
Heres the class I've added it to...
#interface GraphController : TKGraphController {
UIActivityIndicatorView *indicator;
NSMutableArray *data; //I've added
NSString *strChartType; //I've added
}
-(void)setContentType:(NSString*)value; //I've added
#end
#implementation GraphController
-(void)setContentType:(NSString*)value { //I've added
if (value != strChartType) {
[value retain];
[strChartType release];
strChartType = value;
NSLog(#"ChartType=%#", strChartType);
}
}
Heres where I'm getting a warning..
UIViewController *vc = [[GraphController alloc] init];
[vc setContentType:myGraphType]; //Warnings on this line see below
[self presentModalViewController:vc animated:NO];
[vc release];
myGraphType if defined in my constant class.
* Warnings *
warning: 'UIViewController' may not respond to '-setContentType:'
warning: (Messages without a matching method signature
I know the first warning appears when you haven't added the method to the implementation. but I have.
Where am I going wrong ?
UIViewController *vc = [[GraphController alloc] init];
means that vc points to an instance of GraphController but the variable itself is of type UIViewController *, and UIViewController doesn’t declare a -setContentType: method.
Replace that with
GraphController *vc = [[GraphController alloc] init];
to tell the compiler you’re working with a GraphController instance, and it will recognise your -setContentType: method.
You just have to let the compiler know that you're working with a class that it knows responds to the method. You can do it a couple of ways but the easiest one if you just want to eliminate the warning is to cast the object in line before you make the method call.
UIViewController *vc = [[GraphController alloc] init];
[(GraphController *)vc setContentType:myGraphType]; //No warning should appear now.
[self presentModalViewController:vc animated:NO];
[vc release];

How to set a delegate in a different class

I'm working with NSXMLParser that parses a xml document. You have to set the delegate which we would be called every time the parser finds an element. The examples I've looked at all set the delegate to be the same class that's createing:
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:filename];
[parser setDelegate: self];
Other examples set the delegate to be the parent. What if I want another class (not related to the same class) to handle the delegate. What is the syntax to do so?
I've done this but it doesn't work.
#interface Util : NSObject <NSXMLParserDelegate> {
//Some code here
}
//functions for the delegate and the implementation on the Util.m
//.
//.
//.
Thx for your answers.
I forgot to say that when calling the delegate I assumed that it would be something like this:
[parser setDelegate:Util];
I assumed this knowing that to set the delegate for the same class the message is:
[parser setDelegate:self];
You have to create the Util object first.
The delegate has to be an actual instance of a class :)
Util* util = [[Util alloc] init];
[parser setDelegate:util];
[util release];

How to call a method from another method in Objective C?

Can someone answer me how to call one method into another in Objective C on Xcode
The basic syntax for calling a method on an object is this:
[object method];
[object methodWithInput:input];
If methods returns value:
output = [object methodWithOutput];
output = [object methodWithInputAndOutput:input];
More Detail
EDIT:
Here is a good example that how to call method from other class:
OBJECTIVE C - Objective-C call method on another class?
Example:
SomeClass* object = [[SomeClass alloc] init]; // Create an instance of SomeClass
[object someMethod]; // Send the someMethod message
You get a pointer to an object that implements the other method and send the appropriate message (e.g. [otherObject doSomething]).
For example:
#implementation view1
(void)someMethod
{
......code of method...
}
#implementation view2
(void)fistMethod
{
view1 *abc = [[view1 alloc]init];
[abc someMethod];
[abc release];
}
I hope you got it.
If you have 2 functions inside class(.m file):
-(void) func1{ }
-(void) func2{ }
If you want to call func2 from func1, you cannot just call func2();
instead just include self
That is:
-(void) func1{
[self:func2];
}