I need to use the same set of code in 4 view controllers. I am writing this code in -(void)viewWillAppear.
Is there any possibility that I can write this code once and use it in all 4 view controllers?
You could make a subclass of UIViewController with the common code, and then change each of your existing UIViewController subclasses to be a subclass of that new class.
You can create a static class and call the method from there:
#interface myClass : NSObject
+(void)myMethod{
#end
+(void)myMethod{
//Do my stuff
}
And then you call the method from wherever you want:
[myClass myMethod];
Write the code in viewWillAppear in a custom class which subclass of UIViewController.
Then create that 4 classes by subclassing of your custom class.
And call [super viewWillAppear].
Related
I have a view controller, which uses a delegate protocol in order to pass back an array of strings. I now have another view controller, which I'd like to use the same protocol, but I've I use it I get a warning in Xcode Duplicate protocol definition of 'SearchDetailsDelegate' is ignored.
I need these two views to pass back an array for the parent view controller to parse. What would be a more appropriate way of achieve what I need to do here? Would key value observing be the way to go here?
You have few options:
rename your protocols to be different.
create an external protocol and adopt that protocol on each view
Add a property to your view called ParentController with a type of it's parent.
#property (strong,nonatomic) ParentViewController *ParentController;
(synthesise that off course)
Then, in your viewController, when you instantiate the view assign the viewController as the parent
YourView *childView = [[YourView alloc]init];
childView.parentController = self;
Now you can add a method in your viewController that can receive the strings array
-(void)setStringsArray:(NSArray*)arr{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
Lastly send the strings array from the view:
[self.parentController setStringsArray:yourArray];
BTW
if you want to know what view send the array you can:
-(void)setStringsArray:(NSArray*)arr fromView:(UIView*)senderView{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
and use
[self.parentController setStringsArray:yourArray fromView:self];
BTW 2
an other option will be to use notifications.
Define the protocol in a separate .h file (new file of objective c protocol) and then include it in the required view controllers.Redefining the same protocol in two different view controllers is not recommended as it has been in your case
I am reasonably new to Xcode and Objective C.
I have successfully loaded all of the GUI objects on one of my views dynamically..
Now i want to repeat the same dynamically loaded content onto all or most of my views..
I have a method in the main view like this:
-(void)loadinfo:(id)sender{
//All dynamically loaded content etc..
}
I currently have the main view calling this method like so.
[self loadinfo];
So now i need to know (without copying and pasting the method into all of my views) how to call the method from the main view into other views?
I hope this all makes sense.
Edit
I am more knowledgeable in PHP so if i was to do the same thing in php i would make a file called functions.php and include that file into all of the pages.. Is it the same concept?
The concept is different from the approach you find with functions in PHP. In Objective C one works with objects. To share behavior (your PHP functions, called methods in Objective C) between objects you need to slot that behavior somewhere into your class hierarchy.
So for your specific case you would implement the loadinfo method in a generic class which is a subclass of NSView, say MyGenericView. Both your view1 and view2 classes would then subclass from that generic class and inherit the loadinfo method.
In case you want to divert from the implementation of loadinfo in your base class, you can override it partially by doing (in view1 or view2):
- (void) loadview {
[super loadview]; // perform the default implementation
[self doSomethingDifferint]; // perform subclass specific stuff
}
... or:
- (void) loadview {
[self doSomethingDifferint]; // perform subclass specific stuff
[super loadview]; // perform the default implementation
}
... or override completely by doing:
- (void) loadview {
[self doSomethingCompletelyDifferent]; // perform subclass specific stuff
}
Just on a side note: it's good practice to follow CamelCase standards when naming your classes and methods in Objective C, so your classes would be View1 and View2 and the method would be loadInfo.
Further, you might want to read up on generic OO principles and Objective C's specific aspects of it to take full advantage of the language and its features.
You should subclass the view that has all your dynamically-loaded UI content. So if you have:
#interface MyCustomView : UIViewController
{
}
#property (strong, nonatomic) UIView *aView;
-(void)someMethod;
#end
You can create a subclass that will inherit all of the properties, methods, etc:
#interface FirstView : MyCustomView
{
}
//all properties and methods of MyCustomView are inherited
#end
Do this for any number of views that you want to create.
I have declared a method in a view controller as:
-(void)rqst_run{
// method login here
}
When I wrote the code below (in the same file) in ViewDidLoad method after [super viewDidLoad];
[self rqst_run];
I get this error:
Method '-rqst_run' not found (return type defaults to 'id')
Any idea how to fix this?
Thx in advance
Stephane
Methods need to be declared before they're used.
You can solve your issue in one of two ways:
Move the declaration of rqst_run to before the declaration of viewDidLoad
Declare rqst_run in the #interface section for your class (either in the .h, or a class continuation in your .m)
The second is the best of those, so you would, for example, add something akin to the below at the top of your .m
#interface MyClass()
- (void) rqst_run;
#end
If you're not familiar with the concept of class continuations, this page offers a reasonable summary; in essence, an #interface section in the .m for declarations that you want kept out of the "public interface" (i.e. the .h)
This may be a silly question but I haven't found any information on it.
Let's say several of the classes in my program derive from 'MySubView' which is derived from another class, UIViewController.
I would declare it like this:
#interface NewViewController : MySubView {
// code ...
}
#end
In the future the client wants a change, and desires another view with a table. So I would need to make another class, called MySubTableView, that is a UITableViewController subclassed from MySubView.
I was thinking this would be easier if I could do something like this:
#interface NewViewController : UITableViewController : MySubView {
// code ...
}
#end
But this doesn't work.
Is there a way to do this with Xcode, or do I have to specifically make the class itself?
EDIT:
I'm not looking for multiple inheritance. A straight inheritance hierarchy would follow:
NewViewController
UITableviewController
MySubView
UIViewController
No, Objective-C doesn't support declaring those kind of (vertical) inheritance chains. You can only specify the direct super class.
Even if it was possible, there would be problems like calling the correct initializers as they won't be called automatically. Consider a hierarchy like A : B : C - now you can initialize B using e.g. [super init] in As initializer, but how would B know what initializer you want it to call for C?
Objective-C doesn't support multiple inheritance... But Objective-C programmers rarely miss it, because you can accomplish many of the same tasks using Categories instead. Read up on Objective-C Categories.
My question is simple actually, how do I create an object to act as a delegate, instead of including the delegate methods in my view?
For example, I have x functionality that requires delegate methods, and they're currently setup to use self as the delegate. I'd like to put those methods in their own object so that the delegate methods can be called and do stuff if the view has ended.
What's the best way?
for example, NSXMLParser delegate methods - they exist, the delegate is defined, but I dont want to call them as self in my view controller... what other option do I have?
You can specify another custom class to handle the delegate methods, if you wish. Simply create a class, call it MyXMLParserDelegate or something similar. Then, all you have to do is tell your NSXMLParser object that it should use an instance of your class as its delegate.
If you are using Interface Builder, add a new object to the XIB file, set its class to MyXMLParserDelegate, and then drag a connection from your NSXMLParser object's delegate selector to the new object.
If you are doing it programmatically, the basic operation looks like this:
MyXMLParserDelegate * myDelegate = [[MyXMLParserDelegate alloc] init];
[someXMLParser setDelegate:myDelegate];
Keep in mind, however, that delegates are not retained, so in order to do this without leaking memory, you should add an ivar of type MyXMLParserDelegate to your viewController class, and then do the following:
// in your #interface block:
{
...
MyXMLParserDelegate * myDelegate;
}
// in your init method:
myDelegate = [[MyXMLParserDelegate alloc] init];
// in your awakeFromNib method (or anywhere else it seems appropriate):
[someXMLParser setDelegate:myDelegate];
// in your dealloc method:
[myDelegate release];
Check out this answer, I think it covers what you need: How to use custom delegates in Objective-C