So, I'm teaching myself iOS development and Objective-C. I completely new to Xcode and Objective-C but not programming.
I might be going about this completely wrong, but I'm trying call a method from a parent view controller from the child view controller. I tried to use delegation, but I ran into the problem of adopting multiple protocols of different types to the same class. Since I have a table view in my parent view controller, I'm unsure on how to adopt my child view controller delegation.
Essentially, I want to adopt both of these at the same time:
#interface MainViewController : NSObject <AddSiteViewControllerDelegate>
and
#interface MainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
I'm new to the language, so this question might have a simple answer.
Since you want both at the same time, it has to be as below
#interface MainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, AddSiteViewControllerDelegate >
UIViewController is a child of NSObject, so by inheriting UIViewController, you would get the behaviors defined in NSObject.
Objective-C supports single inheritance only. That is, a given class can derive from only one other class. However, a class can implement any number of protocols. So a simple solution to your problem would be:
#interface MainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, AddSiteViewControllerDelegate>
Related
In order to fix bug I need to change a UIViewController into a UITableViewController. Only thing is that the current viewController is a subclass of a base UIViewController. How can I inherit behavior from base? Creating a whole other BaseTableViewController sounds very redundant.
I have many viewcontrollers which needs to have some common functionality related to navigation.
Earlier I made a base class BaseViewController(extending UIViewController) which have all common functionality (like doing some tasks on viewDidLoad etc) and all my viewcontrollers extends BaseViewController.
The problem is that some of my viewcontroller should be subclass of UIViewController and some of UITableViewController, so I can not use above approach.
One way could be to write base class for both and duplicating code. Is there any better way without duplicating code.
While you can get around this by using delegation or helper objects, I would make the case for just not using UITableViewController. It is only a very light subclass on top of UIViewController, providing a table view, conforming to the delegate & data source protocols, and adding a property or two for selection & refresh.
While I wouldn't normally suggest recreating something that the framework has already done for you, it may (in your case) make your code more easy to understand if you just keep everything inheriting from a common base class and add a table view to one of the subclasses.
If you do think this would be a reasonable approach, the UITableViewController documentation overview gives a detailed description of exactly what & where these behaviours are implemented, so mimicking its exact setup is trivial.
Adding a table view to UIViewController
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) IBOutlet UITableView *tableView;
#end
In your storyboard, drag a "Table View" from the object library and drop it on top of your View Controller scene's "View" in the Document Outline - this will replace the root view with a UITableView.
Then just hook it up:
ctrl-drag from the view controller to the table view to hook up the view and tableView outlets
ctrl-drag from the table view to the view controller to set the delegate and dataSource outlets.
Done - no magic required.
I have the following situation of my app. Four child VCs need to have a data source delegate, which their root VC (they all share one root VC) must implement.
My question is - do I declare a protocol in each child VC and implement each protocol individually in the root VC, or is there a better way? Somehow define the protocol only once?
Thanks
No need to declare 4 protocols in 4 child VC's.
Take only one protocol just like #elio.d said and implement that protocol method in your Root VC.
What about having each Child that Inherit from a controller that declare the protocol you need?
#protocol VCChildDelegate <NSObject>
-(void) yourMethod;
#end
#interface WCChildAbstract : UIViewController
#property (nonatomic,assign> id<VCChildDelegate> delegate
#end
I'm trying to add the ECSlidingViewController in my project, but I'm a novice on iOS and I'm not sure what to do in order to follow the instructions: "Add a UIViewController to your storyboards and set the subclass to ECSlidingViewController"
I've added the UIViewController, but now how do I set the subclass?
I don't use storyboards, but it sounds like you have to set custom class for this controller in identity inspector.
Suppose, you added an UIViewController in StoryBoard named "InitialSlidingViewController".
Then in InitialSlidingViewController.h, you should make the interface declaration as follows.
#interface InitialSlidingViewController : ECSlidingViewController
And please check the demo given by "ECSlidingViewController". You will understand it then.
I think you've got class and subclass the wrong way around.
What you are creating is a class called ECSlidingViewController that is a subclass of UIVIewController.
In you ECSlidingViewController.h you should have the following...
#interface ECSlidingViewController : UIViewController
This means you are defining a class called ECSlidingViewController and it is a subclass of UIViewController.
I need to add a common method to all my view controllers no matter what type they are. I notice though that some of my viewcontrollers inherit from UIViewController and some from UITableViewControllers.
How do I write a subclass that can be used for both?
UITableViewController inherits from UIViewController, so if you want to be able to call your custom method from both, you can write a category on UIViewController and then call methods from that category in any subclass of either UIViewController or UITableViewController. Here's a link to Apple's docs on categories and extensions. http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html
if you add a category to UIViewController, you will be able to call those methods on UITableViewController subclasses, as UITableViewController is a subclass of UIViewController