Objective-C inheritance [duplicate] - ios

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.

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
//---------
}

Pass data back to previous view controller causing eroor?

I have a view controller HomeController & BookController.I am going from view controller HomeController to BookController.I am passing data back to previous view controller.So i have used this approach.
#import <UIKit/UIKit.h>
#import "BaseController.h"
#import <MediaPlayer/MediaPlayer.h>
#protocol HomeProtocol
- (void)setComment:(BOOL)data;
-(void)setCommnetArray:(NSMutableArray*)data;
#end
#protocol BookProtocol
-(void)setBook:(BOOL)status;
#end
#interface HomeViewController : BaseController<UITableViewDataSource,UITableViewDelegate,HomeProtocol,UIScrollViewDelegate,BookProtocol,UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UITableView *table_view;
#property BOOL hasUserPostedComment;
#property NSInteger commentIndex;
#property (weak, nonatomic) IBOutlet UILabel *label_post_status;
#property UIRefreshControl *refreshControl;
#property NSInteger start_offset;
#property NSInteger end_offset;
#property (weak, nonatomic) IBOutlet UIButton *btn_refresh;
#property NSMutableArray *temp_user_cooments;
#property MPMoviePlayerController *moviePlayerController;
#property BOOL isBookMarkLoaded;
#end
BookMarkController.h
#import <UIKit/UIKit.h>
#import "BaseController.h"
#import "HomeViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#protocol HomeProtocol
- (void)setComment:(BOOL)data;
-(void)setCommnetArray:(NSMutableArray*)data;
#end
#interface BookMarkController : BaseController<UITableViewDataSource,UITableViewDelegate,HomeProtocol,UIScrollViewDelegate>
#property (nonatomic, weak) id<BookProtocol> myDelegate;
#property (weak, nonatomic) IBOutlet UITableView *table_view;
#property BOOL hasUserPostedComment;
#property NSInteger commentIndex;
#property (weak, nonatomic) IBOutlet UILabel *label_post_status;
#property UIRefreshControl *refreshControl;
#property NSInteger start_offset;
#property NSInteger end_offset;
#property (weak, nonatomic) IBOutlet UIButton *btn_refresh;
#property NSMutableArray *temp_user_cooments;
#property MPMoviePlayerController *moviePlayerController;
#property NSString *index;
#end
Method implementation
-(void)setBook
{
NSLog(#"set book called");
self.isBookMarkLoaded=true;
}
It gives error Unrecognized selector sent to instance.Please tell me what is the issue here.
#protocol BookProtocol
-(void)setBook:(BOOL)status;
#end
Add above protocol to BookMarkController.h. Create delegate of the same in BookMarkController.h file. Make a call to -(void)setBook:(BOOL)status; from BookMarkController.m file
Add delegate in HomeViewController.h file. Define -(void)setBook:(BOOL)status; in HomeViewController.m file and also assign delegate to self.
That's all.

Cannot specify setter 'setTitle:' for properties of NSObject or WKInterfaceController

I am trying to set up a WKInterfaceTable. I followed Apples docs. My Label is not getting text and I get this error message in console:
Cannot specify setter 'setTitle:' for properties of NSObject or WKInterfaceController
How can I fix this? Objective-C please. Here s my code:
.h
#import <Foundation/Foundation.h>
#import <WatchKit/WatchKit.h>
#interface MainRowType : NSObject
#property (strong, nonatomic) IBOutlet WKInterfaceImage *image;
#property (strong, nonatomic) IBOutlet WKInterfaceLabel *title;
#end
.m
#import "MainRowType.h"
#implementation MainRowType
#end
.h
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import "MainRowType.h"
#interface WatchTableController : WKInterfaceController
#property (strong, nonatomic) IBOutlet WKInterfaceTable *tableView;
#property (strong, nonatomic) NSMutableArray *titleArray;
#end
.m
I call this method
- (void)configureTableWithData:(NSMutableArray*)dataObjects {
[self.tableView setNumberOfRows:[dataObjects count] withRowType:#"mainRowType"];
for (NSInteger i = 0; i < self.tableView.numberOfRows; i++) {
MainRowType* theRow = [self.tableView rowControllerAtIndex:i];
NSString *titleString = [dataObjects objectAtIndex:i];
[theRow.title setText:titleString];
}
}
Thanks
Your title property is the problem. You can't name a property "title" in those classes. Change the property name and you should be all set.

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

Weird unknown type name issue

So I have this -
#import <UIKit/UIKit.h>
#import "RateView.h"
#interface BasicCardViewController : UIViewController<RateViewDelegate>
#property (weak, nonatomic) IBOutlet UILabel *label;
#property(copy, nonatomic)NSString *message;
#property(atomic)NSInteger rating;
#property (strong, nonatomic) IBOutlet RateView *rateView;
#property (weak, nonatomic) IBOutlet UILabel *ratingLabel;
#end
and this - in my RateView.h file.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#class RateView;
#protocol RateViewDelegate
-(void)rateView:(RateView *)rateView ratingDidChange:(float)rating;
#end
#interface RateView : UIView
#property(strong,nonatomic)UIImage* fullSelectedStar;
#property(strong,nonatomic)UIImage* notSelectedStar;
#property(strong, nonatomic)UIImage* halfSelectedStar;
#property (assign, nonatomic)float rating;
#property(assign) BOOL editable;
#property (strong) NSMutableArray* imageViews;
#property(assign,nonatomic) int maxRating;
#property(assign) int midMargin;
#property(assign)int leftMargin;
#property (assign) CGSize minImageSize;
#property (assign) id <RateViewDelegate> delegate;
#end
But I get two errors -
1.Cannot find protocol declaration for 'TooviaRateViewDelegate'
2.Unknown type name "RateView"
I've tried to clean, and I've verified that the files are where they should be (their filepaths are to the project).
Why is this happening?
Edit - my AppDelegate.h
#import <UIKit/UIKit.h>
#import "SearchViewController.h"
#interface TooviaAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property NSOperationQueue *queue;
#property (strong, nonatomic) NSDictionary *userProfile;
#property (strong, nonatomic) SearchViewController *searchViewController;
- (NSOperationQueue*) getOperationQueue;
- (id)getSettings:(NSString *)keyPath;
- (void) saveCookies;
#end
There are also issues in my SearchViewController
#import <UIKit/UIKit.h>
#import "BasicCardViewController.h"
#interface SearchViewController : UIViewController
<UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>
- (IBAction)searchButtonClicked:(UIButton *)sender;
- (IBAction)backgroundTap:(id)sender;
#property (weak, nonatomic) IBOutlet UITableView *resultTable;
#property (weak, nonatomic) IBOutlet UITextField *searchBar;
#property (strong, nonatomic) NSArray *resultsTuples;
#property (weak, nonatomic) IBOutlet UIButton *searchButton;
#property (copy, nonatomic) NSArray *controllers;
#property(strong,nonatomic)BasicCardViewController *detailController;
Delegate and data source properties are usually marked as weak for the object graph management reasons described earlier, in “Avoid Strong Reference Cycles.”

Resources