best way to make base uiviewcontroller iphone - iphone

I an making an app which has many different uiviewcontrollers but they share the same functionality. I am going to make a basecontroller so the other classes extend this. However there are about 5-7 variables that I need to provide to the basecontroller. What is the proffered way to do this? Making a constructor that takes 7 parameters or is there better ways doing this?
Thanks in advance.

Subclassing will do the trick:
//different controllers that you want to implement
//it should subclass the base controller, as in this case, the SecondLevelViewContr
#interface CheckListController : SecondLevelViewController {
//and remember to implement your base controller
#interface SecondLevelViewController : UITableViewController {

Related

iphone development: is it possible to extend more than one viewControllers?

in my app I want to use googleAnalytics. To use it I have to extend GAITrackedViewController but the problem is I already extend GLKViewController because my view has an openGL application. So is it possible to extend the properties of both view controllers?
For a similar case, I've simply created a subclass of UIViewController (GLKViewController in your case). That subclass handles the tracking of the view. All "specific" ViewControllers extend that custom UIViewController, instead of the default one.
Then you could, for instance, track the view manually:
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker sendView: NSStringFromClass(self.class)];
No, there isn't multiple inheritance in Objective-C. You have write a subclass of GAITrackedViewController and a subclass of GLKViewController separately, and write a controller class that has an instance of these two classes, coordinating them.
It's not possible, objective-C doesn't support multiple inheritance. You should take a look to this question: Objective-C multiple inheritance
it is very bad to extend 2 classes even on languages that allow it,
because you can get 2 ways to your "super" and this is a way to many bugs

Obj-C, Property 'navigationController' not found on object of type, from NSObject class?

I'm getting the following error
Property 'navigationController' not found on object of type
I've inherited this project and not sure what's going on.
In my m file in didSelectRowAtIndexPath I have
[self.navigationController pushViewController:nextController animated:YES];
It wasn't a problem before as I was accessing app delegate navigation controllers, which were outlets. However, I've had to move away from this approach as it's causing me problems. I've converted the rest of my project. But in this circumstance, where the project isn't using a normal table view, the navigation controller doesn't look to be available. I have this issue in 7 other views. Hoping I could have fixed this, and keep this cleaner code?
I'm really puzzled by this, I think this is occuring as SetsStyleSectionController isn't a view controller but is NSObject.
However, even with this set to UIViewController, the code runs, but doesn't push either.
Changing SetsSectionController from NSObject to UIViewController isn't available.
I'm not sure how to proceed?
I'm in the process of moving away from pushing from app delegate.
Edit: Screenshot 2 discussed below
I see a couple of issues here. You have a misunderstanding about protocols and classes, and you also have an application that interface with a protocol that while well-intentioned is actually making your life much harder than it needs to be.
The first issue you're dealing with is some troubles grokking the difference between protocols and classes, and between adopting a protocol and inheriting from a class. Which is totally fine, this stuff isn't easy. Basically, a protocol is just an interface to an object, and a class provides both an interface and an implementation. In other words, a protocol is just a list of methods that can be called, and a class is both a list of methods and the code to execute those methods. To get a more complete explanation, perhaps you'll be better off going straight to the source - Apple's "The Objective-C Programming Language" will probably help you, so please read about classes and protocols there. I think having done that you'll see why you're not having success giving your id<SetSectionController> instance a navigationController property without explicitly defining one. But do let me know if you have specific questions about this afterwards.
The problem that's harder to fix is this SetSectionController protocol. It has several issues and describing them all is outside the scope of this answer. Here's the thing - the implementation basically requires objects that implement this protocol to know which navigation controller is associated with the table view. This has been up to now provided deus ex machina by coupling them to the application's delegate, and you are right to remove this coupling. But now you have to find another way to get the right data populated into the view controller to push it on the navigation stack.
I think you should move the push logic into the view controller, and for now have the section controller provide an interface that gives the view controller the information it needs. So say the section controller has an implementation like this pseudocode:
- (void)...didSelectRow...
{
id detailsForIndexPath = self.dataForRows[indexPath.row];
DetailViewController *vc = [DetailViewController new];
vc.details = detailsForIndexPath;
[APPDELEGATE.navigationController push:vc];
}
Then I'd add a method to SetSectionController called something like -dataForRow: , the implementation of which would be like the first line of the method above. Then, in your view controller, implement ...didSelectRow... like this:
- (void)...didSelectRow...
{
id<SetSectionController> sc = self.sectionControllers[indexPath.section];
id details = [sc dataForRow:indexPath.row];
DetailViewController *vc = [DetailViewController new];
vc.details = details;
[self.navigationController push:vc];
}
If your section controller is doing anything else useful in ...didSelectRow... make sure to either move it to the view controller or forward ...didSelectRow... on to the section controller for now.
While I do appreciate trying to make complex table sections easier to manage through polymorphism, this protocol wasn't the right way to do it. It blindly copies methods out of UITableViewDelegate and UITableViewDataSource without consideration of whether those are the right questions to be asking something responsible for a single section of a single table. If you still want to use it, I think it will take some significant refactoring to get it into a shape that actually makes your life easier rather than harder. Depending on the complexity of the per-section logic deviation, I might just scrap it altogether. But that's a whole other question really. Hope this helps!
What do you mean it "isn't available"? Do you mean you don't want to/aren't allowed to subclass UIViewController, or are you getting a compiler error? From your comment on your question:
SetsSectionController.h:12:34: Cannot find protocol declaration for 'UIViewController'
you are changing the wrong thing to alter your subclass. As an example:
#import <Foundation/Foundation.h>
#protocol foo <NSObject>
- (void) bar;
#end
#interface lolcats : NSObject <foo>
#end
To change your superclass you change the class name after the colon. Ex.
#interface lolcats : NSObject <foo>
becomes
#interface lolcats : UIViewController <foo>
and you're off and running. However, if you accidentally change the protocol requirement for the protocol
#protocol foo <NSObject>
to
#protocol foo <UIViewController>
instead, or change the protocol you adhere to to <UIViewController>, you'll end up getting the EXACT error you got. You might be confused because the protocol says the object adhering to it must also adhere to the NSObject protocol, and NSObject is also a type of object. The object and protocol are separate things, and all objects inherit from NSObject, and thus inherit the protocol. Basically, it's saying "objects using this protocol must be objects."
Why did you expect this to work? The object is just a standard NSObject that states it adheres to a protocol containing a few methods. Why would it have a navigation controller property? It hasn't inherited it from anything. Regardless, based on your error, you probably just changed the wrong thing. Make sure you change the superclass class name after the colon.

Adding Two different Delegates in iPhone

I am new to iphone app development. I want to add two different delegates.
I know how to add multiple delegates of same class like
UIViewController .
I want like this
#interface HelloWorld : CCLayer, UIViewController
in which one class is CCLayer of cocos2d and second one simple UIViewController.
Thanks in advance for help.
I'm not familiar with CCLayer but you can't do multiple inheritance in Obj-C (so you can't have two superclasses). What you can do is create two classes, one that subclasses UIViewController and the other subclasses CCLayer and then create a protocol/delegate pattern to allow communication between the two.
http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

Possible to use UIActionSheet from Application Delegate?

I have a common UIActionSheet which I use across about 10 different view/view controllers. I'm wondering if it's possible to use UIActionSheet from the app delegate in order to prevent code duplication?
So far my attempts to use an action sheet from the delegate haven't worked, I suspect my problem lies when calling the showInView method - do I need to instantiate an object of my view controller then use viewController.view here? If so how can I then tell which view called the action sheet method from the delegate?
I didn't try the approach proposed by c_phlat, but I wonder to what self.view is mapped.
I did manage to do it like this:
[actionSheet showInView:window];
it works just as well.
I was having the same problem, and I recently figured out a way to fix it in my app. The key for me was to make my app delegate class an extension of UIViewController rather than NSObject. (I think UIViewController is a subclass of NSObject anyway, so this shouldn't affect your app too much.)
In other words, change the main implementation line in your app delegate interface file from something like this:
#interface YourAppDelegate : NSObject <UIApplicationDelegate, UIActionSheetDelegate> {
To this:
#interface YourAppDelegate : UIViewController <UIApplicationDelegate, UIActionSheetDelegate> {
You should now be able to use the showInView: method with your action sheet within your app delegate implementation:
[yourActionSheet showInView:self.view];

three20 - loading external Data for TTViewController

i created a TTViewController that should show some Informations from a external (web) source.
i already worked with some TTableViewControllers where i used the TTModel System (i created a TTURLRequestModel and a TTListDataSource for my TTTableViewControllers)
whats the way to do this with a TTViewController?
is there also something like a DataSource and Request Model or do i have to assign my ViewController with the TTURLRequestDelegate and run a simple URL Request?
im asking because i dont know whats the "best" and "newest" way to fill my viewcontroller with data from the web. i know how to do this for tableviews but not for views.
thanks in advance
edit: some code
my singlePostViewController:
#interface singlePostViewController : TTModelViewController
singlePostModel *_singlepostmodel;
#end
- (void) createModel {
_singlepostmodel = [[singlePostModel alloc] init];
}
and my singlePostModel
#interface singlePostModel : TTURLRequestModel {
}
#end
but what to do now? where to load my stuff and setting it up?
Check out the TTModelViewController class. You'll want to subclass that. You create the TTURLRequestModel in the createModel: method and add "self" to the list of model's delegates. There are a bunch of interesting methods available that should be sufficient for what you're trying to do.
In particular, this PDF shows a good relationship between the model, datasource, and controller classes.
Check this twitter example
https://github.com/facebook/three20/tree/master/samples/TTTwitter
Its straightforward example.
You need to override "-(void) load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more" method in your subclass of TTURLRequestModel to fetch remote data.