Inheritance on view controller Objective c - ios

I am trying to create a generic view controller and 2 other views which both will inherit from.
Now this is the generic view (what i think is relevant - if something else is needed i will add it):
#interface CardGameViewController ()
#property (strong, nonatomic) IBOutlet UITabBarItem *TabSelection;
#property (strong, nonatomic) CardMatchingGame *game;
#property (strong, nonatomic) IBOutlet UIButton *resetButton;
#property (weak, nonatomic) IBOutlet UILabel *scoreLable;
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
#end
#implementation CardGameViewController
- (CardMatchingGame*)game
{
if(!_game) _game = [[CardMatchingGame alloc]initWithCardCount:[self.cardButtons count] usingDeck:[self createDeck] gameMode:[self getActiveTabWithString:self.TabSelection.description]];
return _game;
}
- (Deck*)createDeck //abstract
{
return nil; <------- Relevant generic function
}
and these are the 2 files which inherit the mentioned file:
SetGameViewController.h:
#import "CardGameViewController.h"
#interface SetGameViewController : CardGameViewController
#end
SetGameViewController.m:
#import "SetGameViewController.h"
#import "SetCardDeck.h"
#interface SetGameViewController ()
#end
#implementation SetGameViewController
-(Deck*)createDeck
{
return [[SetCardDeck alloc]init];
}
#end
and the second one:
PlayingCardGameViewController.h:
#import "CardGameViewController.h"
#interface PlayingCardGameViewController : CardGameViewController
#end
PlayingCardGameViewController.m:
#import "PlayingCardGameViewController.h"
#import "PlayingCardDeck.h"
#interface PlayingCardGameViewController ()
#end
#implementation PlayingCardGameViewController
- (Deck*)createDeck
{
return [[PlayingCardDeck alloc]init];
}
#end
I also have tab bar navigator to switch between the 2 views.
The thing is, the first (SetGameViewController) is never instantiated no matter what i do,
Meaning the createDeck function is never called,
While the second one (PlayingCardGameViewController) is ok every time.
What i have tried:
placing break points on the main view - (CardMatchingGame*)game function.
this one only gets called when i am trying to start the second working view, and not the bad one.
If there is anything missing to give a certain lead, please let me know and i will add it.

Set breakpoints on the empty createDeck method in the base class, as well as on the method in your two subclasses.
My guess is that when you set up your tab bar controller in IB (Interface Builder) , you made one of the tabs be an instance of the generic CardGameViewController instead of a SetGameViewController.

Related

Cannot find delegate defined in another header

I have a very simple problem, yet because it's so simple, I can't find anything wrong with it to fix.
ListSelectorUtility.h
#import <UIKit/UIKit.h>
#protocol ListSelectorDelegate <NSObject>
- (void) returnValueString:(NSString *)value requestID:(long)requestID;
#end
#interface ListSelectorUtility : UITableViewController
#property (strong, nonatomic) NSString *data;
#property (weak, nonatomic) id<ListSelectorDelegate> delegate;
#property (nonatomic) long requestID;
#end
UpdateProperty.h
#import <UIKit/UIKit.h>
#import "ListSelectorUtility.h"
#interface UpdateProperty : UITableViewController <ListSelectorDelegate>
#property (nonatomic, strong) NSDictionary *data;
#end
There's an error on the #interface UpdateProperty : UITableViewController <ListSelectorDelegate> saying that No type or protocol named 'ListSelectorDelegate'. Why is this happened? How to solve this? Thanks.
EDIT: oh, and when I do CMD+click on the <ListSelectorDelegate>, it brings me straight up to the ListSelectorDelegate declaration. That means the IDE (supposedly) can find it.
set property of delegate (strong) on ListSelectorUtility.h
#property (strong, nonatomic) id<ListSelectorDelegate> delegate;
may bey problem of that because if it is week then dealloc by ARC and call the delegate method on ListSelectorUtility.h on your pass the data and values
[self.delegate returnValueString:obj_StringValue requestID:obj_requestID];
And finally this delegate method declare in UpdateProperty.h,
- (void) returnValueString:(NSString *)value requestID:(long)requestID
{
//---------
//Your Code
//---------
}

Duplicate interface definition for class "ViewController"

At the last line, I got the error: Duplicate interface definition for class "ViewController". I want to do an IBAction. What is the fault? What can I do? Please help me.
//
// ViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface SimpleEmailViewController : UIViewController <MFMailComposeViewControllerDelegate>
- (IBAction)showEmail:(id)sender;
#end
#interface ViewController : UIViewController <MFMessageComposeViewControllerDelegate> {
}
-(IBAction)sendMessage:(id)sender;
- (IBAction)showEmail:(id)sender;
#property (nonatomic, strong) IBOutlet UIWebView *site ;
- (IBAction)call:(id)sender;
#property (retain, nonatomic) IBOutlet UIButton *myBotton;
#end
#interface ViewController : UIViewController**i**
The problem is exactly what the error states. You defined #interface viewController twice. Change the name of one to something else. As a side note, it is a terrible idea to name something with a name apple has already used. You should change both viewControllers to something else, more descriptive of what it does, like mailViewController or setupViewController. Weird stuff can happen when you use apple defined names.
#interface FSMainiPadViewController : UIViewController
Why don't you just make it simple like this?
// ViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface ViewController : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
// Actions
- (IBAction)showEmail:(id)sender;
- (IBAction)sendMessage:(id)sender;
- (IBAction)call:(id)sender;
// Properties
#property (nonatomic, strong) IBOutlet UIWebView *site;
#property (retain, nonatomic) IBOutlet UIButton *myBotton;
#end

How to change page with UISlider from an UIView

To summarize my app, it's like an iBook : I have multiple pages filled in with an xml file (text and image).
Like in the iBook app, I want to use a UISlider to select and change page.
I use a UIViewController (appViewController) as contener of the different UIViews (contentViewController)
header of AppViewController :
#import <UIKit/UIKit.h>
#import "contentViewController.h"
#interface AppViewController : UIViewController <UIPageViewControllerDataSource>
{
UIPageViewController *pageController;
NSArray *pageContent;
NSDictionary *pageFields;
}
in AppViewController.m, I have :
// Create a new view controller and pass suitable data.
contentViewController *dataViewController = [[contentViewController alloc]initWithNibName:#"contentViewController" bundle:nil];
The header of the contentViewController is :
#import <UIKit/UIKit.h>
#class DCIEquipment;
#interface contentViewController : UIViewController
#property (nonatomic,strong) DCIEquipment *eqt;
#property (assign, nonatomic) NSUInteger pageIndex;
#property (assign, nonatomic) NSUInteger pageNumber;
#property (strong, nonatomic) IBOutlet UISlider *pageSlider;
#end
Because in iBook, the slider seems to follow the curling movement of the page, I put my UISlider on the contentViewController. Everything works good with the UISlider but I don't how know to do to informe the UIViewController to change the page (I assumed that the code of changing the page has to be done in this controller).
Please tell me if you need some more code. I hope that explainations are clear. I juste want to know where to put the UISlider (which view) and how to send value to the pageViewController.
Thank you in advance.
Raphael
Use the protocols. There are lots of examples. And you are using at least one of them (UIPageViewControllerDataSource).
Just create one for your contentViewController (in it's header file)
#class ContentViewController; // this is fast-forward declaration
#protocol ContentViewControllerDelegate
- (void)contentViewController:(ContentViewController *)controller didChangePage:(NSUInteger)pageIndex;
#end
#interface ContentViewController : UIViewController
// ...
#property (weak,nonatomic) id<ContentViewControllerDelegate> delegate; // don't forget to set the value with your appViewController
// ...
#end
In your ContentViewController-implementation file in some method just call the delegate method
if (self.delegate && [self.delegate respondsToSelector:#selector(contentViewController:didChangePage:)])
[self.delegate contentViewController:self didChangePage:newIndex];
And don't forget to implement this method in appViewController class.

Objective-C inheritance [duplicate]

This question already has answers here:
Objective-C: How do you access parent properties from subclasses?
(5 answers)
Closed 9 years ago.
I'm making a very simple app that has lots of different quizzes. My motivation is primarily to learn Objective-C and iOS development. So I have a few views in my app, all of them have the same background which I set programmatically. I decided to make a subclass of UIViewController which added the function setBackground, and make all ViewControllers in my app inherit this subclass (SumsViewController).
This worked fine, but now I'd like to take out some of the stuff that's common to each quiz and add that in to a subclass of SumsViewController, called QuizController. Having tried this I'm getting errors about property x not found on object of type y.
Here's the code:
SumsViewController.h
#import <UIKit/UIKit.h>
#interface SumsViewController : UIViewController
- (void)setBackground;
#end
SumsViewController.m
#import "SumsViewController.h"
#interface SumsViewController ()
#end
#implementation SumsViewController
- (void)setBackground
{
... //Code to set Background
}
#end
QuizController.h
#import <UIKit/UIKit.h>
#import "SumsViewController.h"
#interface QuizController : SumsViewController
#property (nonatomic) NSInteger number1;
#property (nonatomic) NSInteger number2;
#property (nonatomic) NSInteger difficulty;
#property (nonatomic) NSInteger score_val;
#property (nonatomic) NSTimer* countdown;
#property (nonatomic) NSInteger time_val;
#end
QuizController.m
#import "QuizController.h"
#interface QuizController ()
#end
#implementation QuizController
#end
Now here's a controller for a quiz that inherits QuizController:
AdditionController.h
#import <UIKit/UIKit.h>
#import "QuizController.h"
#interface AdditionController : QuizController
#end
AdditionController.m
#import "AdditionController.h"
#import "ViewController.h"
#interface AdditionController ()
#property (weak, nonatomic) IBOutlet UILabel *question;
#property (weak, nonatomic) IBOutlet UILabel *timer;
#property (weak, nonatomic) IBOutlet UIButton *ans1;
#property (weak, nonatomic) IBOutlet UIButton *ans2;
#property (weak, nonatomic) IBOutlet UIButton *ans3;
#property (weak, nonatomic) IBOutlet UILabel *score;
#end
#implementation AdditionController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setBackground]; //function declared in SumsViewController WORKS FINE
self.difficulty = 20; //declared in QuizController DOES NOT WORK
// Error says property not member of object AdditionController
[self setupGame];
}
#end
I'd appreciate any help on this, I'm sure it's a simple misunderstanding but this is frustrating me.
Try cleaning you project and re building files ( Command + Shift + K ).
I'm suggesting that because I just copied your code in a sample project, and it works fine on my computer.

Declared Interface errors when building the project

I im fairly new to ios development, and following the tutorial exercise on chapter 4 of the book 'beginning ios5 development' i have run into some compile errors on my code.
here's what my header code .h file looks like
#import <UIKit/UIKit.h>
#interface BIDViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *namefIELD;
#property (strong, nonatomic) IBOutlet UITextField *numberField;
#property (strong, nonatomic) IBOutlet UILabel *sliderLabel;
#property (strong, nonatomic) IBOutlet UISegmentedControl *leftSwitch;
#property (strong, nonatomic) IBOutlet UISegmentedControl *rightSwitch;
#property (strong, nonatomic) IBOutlet UIButton *doSomethingButton;
-(IBAction)textFieldDoneEditing:(id)sender;
//to initialize the 'done' button when editing to confirm you have finished editing/
-(IBAction) backgroundTap:(id)sender;
- (IBAction)sliderChanged:(id)sender;
- (IBAction)switchChanged:(id)sender;
- (IBAction)toggleControls:(id)sender;
- (IBAction)buttonPressed:(id)sender;
#end
and here's my .m code where im facing the error "no visible #interface for 'UISegmentedControl' declares the selector 'setOn:animated;'
#synthesize leftSwitch;
#synthesize rightSwitch;
- (IBAction)switchChanged:(id)sender {
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[leftSwitch setOn: setting animated: YES]; //the error lies here/
[rightSwitch setO: setting animated: YES]; //the error lies here/
}
- (IBAction)toggleControls:(id)sender {
}
- (IBAction)buttonPressed:(id)sender {
}
#end
the error statement is "no visible #interface for 'UISegmentedControl' declares the selector 'setOn:animated;'
pls help, that would be highly appreciated and voted for :)
#PhilipMills ...here's what i'm talking about, as soon as i click on a leftside or rightside of the segmented control object a break point is automatically made on my delegated header code here
#import <UIKit/UIKit.h>
#import "BIDAppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([BIDAppDelegate class])); //the breakpoint appears here.
}
}
A UISwitch has a setOn:animated: method but a UISegmentedControl does not. I'll guess that you assigned objects to leftSwitch and rightSwitch that are not the kind you intended (or the tutorial intended).

Resources