#class and #import on another view controller, what does it means? - ios

I am newbie on Xcode, and trying to figure out more about coding in xcode.
So, I am trying to learn more about models (models operation) on objective C.
I am confused in #Class declaration in PhotoViewController.h and .m Files
as you may see below, I already imported Photo.h on appdelegate.m and also PhotoViewController.m files
the objective from my tutorial is PhotoViewController.m files can recognize self.photo.filename
But, why it has to add #Class and #property in PhotoViewController.h files?
isnt #import command is already enough? what does #Class means and why it has to include #property too?
note : I tried to put a comment (//) on #class , but xcode tell me that photo property not found, and when I put added comment (//) on property
PhotoViewController.m file also messed up with unrecognized photo property.
I dont quite understand, the use of #class and #import at the same time, plus declaring #property photo
here is Photo.m
#import "Photo.h"
#implementation Photo
-(id)init
{
self = [super init];
return self;
}
#end
and
Photo.h
#import <Foundation/Foundation.h>
#interface Photo : NSObject
#property (weak, atomic) NSString *title;
#property (strong, nonatomic) NSString *detail;
#property (strong, nonatomic) NSString *filename;
#property (strong, nonatomic) NSString *thumbnail;
#end
Appdelegate.m
#import "AppDelegate.h"
#import "FeedTableViewController.h"
#import "ProfileViewController.h"
#import "FavoritesViewController.h"
#import "Photo.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Photo *photo= [[Photo alloc]init];
photo.title = #"Demo Photo";
photo.detail = #"This is a demo photo";
photo.filename = #"demo.png";
photo.thumbnail = #"demo-thumb.png";
return YES;
}
#end
PhotoViewController.h Files
#import <UIKit/UIKit.h>
#class Photo;
#interface PhotoViewController : UIViewController
#property (weak, nonatomic) NSString *imageFileName;
#property (weak, nonatomic) NSString *imageTitle;
#property (strong, nonatomic) Photo *photo;
#end
PhotoViewController.m Files
#import "PhotoViewController.h"
#import "UIImageView+AFNetworking.h"
#import "Photo.h"
#implementation PhotoViewController
-(void)viewDidLoad {
// self.title = self.imageTitle;
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImageWithURL:[UIImage imageNamed:self.photo.filename]];
imageView.frame = CGRectMake(10,10,300,300);
[self.view addSubview:imageView];
UILabel *imageTitleLabel = [[UILabel alloc] init];
imageTitleLabel.text = self.imageTitle;
imageTitleLabel.frame = CGRectMake(11,320,300,40);
[self.view addSubview:imageTitleLabel];
}
#end

#class Photo defines the existence of class Photo to PhotoViewController.h allowing you to declare photo property.
Photo property is later used in PhotoViewController.m to to access the instance variable photo like this self.photo or [self photo]
You could have put #import "Photo.h" in your PhotoViewController.h but it is cleaner this way :)

#property
It is for replacement of getter method, whenever you want to get the value you have to declare that variable as property, so that need not to write getter method separately,
you should implement #synthesize also in Photo.m, #synthesize will work as setter method.

Related

Class doesn't seem to work. Wont return image

I'm pretty new to classes in objective-c. I'm testing out one I made unhiding an image from the class on the click of a button, everything compiles and runs but it's not working out for me, nothing shows up when I click. Am I doing this all wrong? Tried to include everything I think is relevant in the code below, obviously the images for the other parts of the class are there but I didn't include them below seeing as I'm just testing one image from it.
question.m file -
#import "Question.h"
#implementation Question
#synthesize image;
#end
question.h -
#import <UIKit/UIKit.h>
#import "ViewController.h"
#ifndef Question_h
#define Question_h
#endif /* Question_h */
#interface Question : NSObject
{
UIImageView *image;
}
#property(nonatomic, retain) UIImageView *image;
#end
#class Question;
#interface Controller : NSObject
{
Question *firstQuestion;
Question *secondQuestion;
Question *thirdQuestion;
}
#property(nonatomic, retain) Question *firstQuestion;
#property(nonatomic, retain) Question *secondQuestion;
#property(nonatomic, retain) Question *thirdQuestion;
-(void) Question;
ViewController.m -
#import "ViewController.h"
#import "Question.h"
#synthesize question1, q2, q3, firstaid;
- (void)viewDidLoad {
[super viewDidLoad];
firstTierQuestions = [[NSMutableArray alloc] initWithCapacity:100];
}
- (IBAction)Question:(id)sender {
Question *firstQuestion = [[Question alloc] init];
firstQuestion.image = question1;
firstQuestion.a1 = _answer1;
firstQuestion.a2 = _answer2;
[firstTierQuestions addObject:firstQuestion];
- (IBAction)generator:(id)sender {
for (Question *firstQuestion in firstTierQuestions) {
[firstQuestion.image setHidden:NO];
}
}
viewController.h -
#import "question.h"
#interface ViewController : UIViewController
{
IBOutlet UIImageView *question1;
NSMutableArray *firstTierQuestions;
}
#property (strong, nonatomic) IBOutlet UIButton *generator;
#property (strong, nonatomic) IBOutlet UIImageView *question1;
- (IBAction)generator:(id)sender;
#property (nonatomic, strong) NSObject *Question;
#end

Copying one NSString into other one Xcode

I have a problem in Xcode.I am trying to copy one NSString into other one but is not working.
The original NSString pageTitle is on the RestViewController:
This is my RestViewContoller.h:
#import <UIKit/UIKit.h>
#import "Restaurant.h"
#interface RestViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
#property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
#property (strong, nonatomic) IBOutlet UIImageView *ImageView;
#property (nonatomic, strong) Restaurant *DetailModal;
#property (nonatomic, strong) NSString *pageTitle;
#property (nonatomic, strong) NSString *pageDescription;
#end
The NSString I want to save the data is on the RestViewController:
#property (nonatomic, strong) Restaurant *DetailModal;
but is a Restaurant class type.
My Restaurant class is:
Restaurant.h:
#import <Foundation/Foundation.h>
#interface Restaurant : NSObject
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *desc;
#property (nonatomic, copy) NSString *image;
- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image;
#end
Restaurant.m:
#import "Restaurant.h"
#implementation Restaurant
- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image {
self = [super init];
if (self != nil) {
self.title = title;
self.desc = description;
self.image = image;
}
return self;
}
#end
And finally the place where I want to copy this NSStrings is my RestViewController.m:
#import "RestViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface RestViewController ()
#end
#implementation RestViewController
- (void)viewDidLoad {
[super viewDidLoad];
_pageTitle = #"Test";
_DetailModal.title = _pageTitle;
NSLog(#"_DetailModal.title: %#", _DetailModal.title);
}
#end
The problem is when I see the result of the
NSLog(#"_DetailModal.title: %#", _DetailModal.title);on the console,
it puts me:2016-11-21 11:42:05.407 MadEat[3667:104028]
_DetailModal.title: (null)
What can I do? I know I have a low level of Xcode, but I need your help please. Thank you very much.
Initailize the instance of DataModel First.
Then proceed with assignment
_DetailModal = [[DetailModal alloc] init:_pageTitle descripiton:#"Desc" image:#"image"];
There is a instance method defined already to do the task.
After this also you can alter the value by
_DetailModal.title = #"title_of_yours"
If you have to use detailmodal, you must alloc the restaurant class object (detailmodal). Then only the objects can be used.
detailmodal=[[restaurant alloc]init];
detailmodal.title=_pagetitle;
Before you can use a object, you have to create it first.
DetailModal *detailModal = [DetailModal new];
That way the object will be created and values stored to the class properties will be reserved.
Try:
_DetailModal.title = [[NSString alloc] initWithString: _pageTitle];
(Did not test this)
plz chek you _DetailModal is not nil
if it's nil then
first you need to alloc the memory for you DetailModal Class object
like this
DetailModal *detailModal = [DetailModal new];
then after set the value you want
detailModal.title = _pageTitle;

Objective-c error with subclass not finding superclass interface

I've been working on an app that uses a collection view, and I'm creating a custom view cell (Category View Cell) which is a subclass of UICollectionViewCell. I also wanted to create a subclass of the custom view cell (LinkCell). I've been searching for a while now and I cannot find why I am getting the error "Cannot find interface declaration of 'CategoryViewCell', superclass of 'LinkCell'"
//CategoryViewCell.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#class ViewController;
#interface CategoryViewCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIImageView *image;
#property (nonatomic) ViewController *parentView;
#property (nonatomic) NSString *cellName;
#end
//CategoryViewCell.m
#import "CategoryViewCell.h"
#implementation CategoryViewCell
#end
//LinkCell.h
#import <UIKit/UIKit.h>
#import "CategoryViewCell.h"
#import "PJP Webview.h"
#interface LinkCell : CategoryViewCell //Error here
#property (nonatomic) NSString *username;
#property (nonatomic) NSString *password;
#property (nonatomic) NSString *urlToLink;
#property (nonatomic) NSString *urlToLinkS;
#property (nonatomic) NSString *urlToLinkP;
#property (nonatomic) NSString *urlToLinkT;
#property (nonatomic) NSString *body;
-(IBAction)celltapped:(id)sender;
#end
//LinkCell.m
#import "LinkCell.h"
#implementation LinkCell
#synthesize urlToLink, username, password, image, cellName, parentView;
-(IBAction)celltapped:(id)sender {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *launchString = [NSString stringWithFormat:#"hasLaunched%#Before", self.cellName];
BOOL hasLaunchedCellBefore = [userDefaults boolForKey:launchString];
if (!hasLaunchedCellBefore) {
// first time launch code
hasLaunchedCellBefore = TRUE;
[userDefaults setBool:hasLaunchedCellBefore forKey:launchString];
[userDefaults synchronize];
PJP_Webview *vc = [parentView.storyboard instantiateViewControllerWithIdentifier:#"vc"];
vc.currentCell = self;
[parentView presentViewController:vc animated:YES completion:nil];
}
else {
}
}
#end
Could someone please show me where my error is?
I just solved it; ViewController.h uses the CategoryViewCell and I imported the .h and used the #class for the ViewController property on CategoryViewCell.h. Only the #class was necessary; I guess some sort of circular dependency was created. The weird part was that CategoryViewCell.h didn't have the errors, its subclass did. Thanks to anyone who answered!
Choose file CategoryViewCell.m and check it's added to target or not.

Property not found although it's in the header

I have one view and one View Controller. I am trying to have a reference to View Controller from the view.
PlusCalendarView.h
#interface PlusCalendarView : TSQCalendarView
#property (nonatomic, strong) UIViewController *initialVC;
#end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpCalendarView:self.myCalendarView];
self.myCalendarView.initialVC = self; // ERROR: Property initialVC not found on object of type "PlusCalendarView"
}
How come it cannot find initialVC property?
Update:
ViewController.h
#import <UIKit/UIKit.h>
#import <TimesSquare/TimesSquare.h>
#interface PlusCalendarView : TSQCalendarView;
#end
#interface ViewController : UIViewController
#property (nonatomic, strong) NSCalendar *calendar;
#property (strong, nonatomic) IBOutlet PlusCalendarView *myCalendarView;
#end
The problem is that you're declaring PlusCalendarView in ViewController.h AND PlusCalendarView.h. You should remove the #interface declaration from ViewController.h.
Instead, add #class PlusCalendarView in ViewController.h then #import "PlusCalendarView.h" in ViewController.m

Receiver 'SMClient' for class message is a forward declaration

I'm trying to add StackMob to my project. It says to create an SMClient instance after having dragged the SDK to the project, checking 'create groups for..' and adding to target. I followed these steps.
However, when I'm creating my SMClient and SMCoreDataStore instances, it gives me an error of Receiver 'SMClient' for class message is a forward declaration and the same for SMCoreDataStore. Here's my code:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#class SMClient;
#class SMCoreDataStore;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (strong, nonatomic) SMCoreDataStore *coreDataStore;
#property (strong, nonatomic) SMClient *client;
#end
And part of my .m:
#import "AppDelegate.h"
#import "StackMob.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.client = [[SMClient alloc] initWithAPIVersion:#"0" publicKey:#"YOUR_PUBLIC_KEY"];
self.coreDataStore = [self.client coreDataStoreWithManagedObjectModel:self.managedObjectModel];
return YES;
}
I already cleaned the project, imported the relevant header files, but it still gives that error.
Any ideas?
This might be happening because you forgot to import the classes.
Add it on your .m:
#import "SMClient.h"
#import "SMCoreDataStore.h"

Resources