Objective C: How to use UIImage as a property in my model? - objective-c

I have a Player model:
Player.h:
#import <Foundation/Foundation.h>
#interface Player : NSObject
#property (nonatomic, strong) UIImage *image;
#property (nonatomic, strong) NSMutableArray *answers;
#end
Player.m:
#import <Foundation/Foundation.h>
#import "Player.h"
#interface Player ()
#end
#implementation Player
- (id)init {
if (self = [super init]) {
self.answers = [[NSMutableArray alloc] init];
self.image = [UIImage imageNamed:#"defaultProfileImage"]; // <-------------- error
}
return self;
}
#end
However this displays the error: Use of undeclared identifier 'UIImage'
How would I use UIImage in my model?

You will need to import
#import <UIKit/UIKit.h>
So that UIImage is recognised

Related

Access variable in once from another class in Objective-C

I have Two classes in have declared string in one variable and trying access from another class. have initiated getter setter in between them still getting null value when I try to print from subclass.
CustomerCarSelection.h
#import <UIKit/UIKit.h>
#interface CustomerCarSelection : UITableViewController <UITableViewDataSource, UITableViewDataSource>
#property (strong, nonatomic) IBOutlet UITableView *carListTableView;
- (IBAction)next:(id)sender;
#property NSString *carSelectedForService;
#end
customerCarSelection.m
#import "CustomerCarSelection.h"
#import <Parse/Parse.h>
#import "carSelectCellTableViewCell.h"
#import "ServiceResultView.h"
#interface CustomerCarSelection ()
#end
#implementation CustomerCarSelection
#synthesize carListTableView;
#synthesize carSelectedForService;
- (void)viewDidLoad {
[super viewDidLoad];
ServiceResultView *service = [[ServiceResultView alloc]init];
service.pointer = self;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cell tabbed");
PFObject *temp = [cellDetails objectAtIndex:indexPath.row];
carSelectedForService = temp.objectId;
NSLog(#"%#", carSelectedForService);
}
serviceResultView.h
#import <UIKit/UIKit.h>
#import "CustomerCarSelection.h"
#interface ServiceResultView : UIViewController
{
CustomerCarSelection *pointer;
}
#property (nonatomic, retain) CustomerCarSelection *pointer;
- (IBAction)confirm:(id)sender;
#end
serviceResultView.m
#import "ServiceResultView.h"
#import "CustomerCarSelection.h"
#interface ServiceResultView ()
#end
#implementation ServiceResultView
#synthesize pointer;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *carResult = pointer.carSelectedForService;
NSLog(#"%#", carResult);
}
Where am I making mistake. When the cell tabbed it will assign object id to the String i trying access that object from another class
CustomerCarSelection.h
#import <UIKit/UIKit.h>
#interface CustomerCarSelection : UITableViewController <UITableViewDataSource, UITableViewDataSource>
#property (strong, nonatomic) IBOutlet UITableView *carListTableView;
- (IBAction)next:(id)sender;
#end
customerCarSelection.m
#import "CustomerCarSelection.h"
#import <Parse/Parse.h>
#import "carSelectCellTableViewCell.h"
#import "ServiceResultView.h"
#interface CustomerCarSelection ()
#end
#implementation CustomerCarSelection
#synthesize carListTableView;
#synthesize carSelectedForService;
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cell tabbed");
PFObject *temp = [cellDetails objectAtIndex:indexPath.row];
serviceResultView *obj = [serviceResultView alloc] init];
obj.carSelectedForService = temp.objectId;
NSLog(#"%#", obj.carSelectedForService);
}
serviceResultView.h
#import <UIKit/UIKit.h>
#import "CustomerCarSelection.h"
#interface ServiceResultView : UIViewController
#property NSString *carSelectedForService;
- (IBAction)confirm:(id)sender;
#end
serviceResultView.m
#import "ServiceResultView.h"
#import "CustomerCarSelection.h"
#interface ServiceResultView ()
#end
#implementation ServiceResultView
#synthesize pointer;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"%#", self.carSelectedForService);
}

Objective C Property Syntax

I have quick question regarding my code:
This is my Animal.h header file:
#import <Foundation/Foundation.h>
#interface Animal : NSObject
#property (nonatomic) int age;
#property (nonatomic, strong) NSString *name;
#property (nonatomic,strong) NSString *breed;
#property (retain, nonatomic) UIImage *image;
-(void) bark;
-(void)barkNumTimes: (int)numOfTimesToBark;
-(void)barknumTimes:(int)numberOfTimes loudly:(bool) isLoud;
-(int) ageInDogYears: (int)humanYears;
#end
For some reason at the line:
#property (retain, nonatomic) UIImage *image;
I get an error saying that "Property with 'retain (or strong)' attribute must be of object type".
My ViewController.m class is where I created three Animal objects and used the UIImage property which I created in Animal.h and set each of the Animal objects UIImage property to a certain image I have in my supporting files:
#import "ViewController.h"
#import "Animal.h"
#import "Puppy.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.whichDog = 0;
Animal *whiteDog = [[Animal alloc]init];
[whiteDog setName:#"white"];
[whiteDog setBreed:#"White Dog"];
whiteDog.image = [UIImage imageNamed:#"whitedog.jpeg"];
Animal *brownDog = [[Animal alloc] init];
[brownDog setName:#"brown"];
[brownDog setBreed:#"Brown Dog"];
brownDog.image = [UIImage imageNamed:#"browndog.jpeg"];
Animal *husky = [[Animal alloc] init];
[husky setName:#"husky"];
[husky setBreed:#"Husky Dog"];
husky.image = [UIImage imageNamed:#"husky.jpeg"];
self.myAnimals = [[NSMutableArray alloc] init];
[self.myAnimals addObject:whiteDog];
[self.myAnimals addObject:brownDog];
[self.myAnimals addObject:husky];
Puppy *pup = [[Puppy alloc]init];
[pup setName:#"coby"];
[pup setBreed:#"Portuguese Water Dog"];
pup.image = [UIImage imageNamed:#"puppy.jpeg"];
}
- (IBAction)newDogBarButton:(UIBarButtonItem *)sender{
int numOfDogs = (int)[self.myAnimals count];
int randomIndex = arc4random() % numOfDogs;
Animal *randomAnimal = [self.myAnimals objectAtIndex:randomIndex];
[UIView transitionWithView:self.view duration:1.5 options:UIViewAnimationOptionTransitionCurlDown animations:^{
self.imageView.image = randomAnimal.image;
self.carNAme.text = randomAnimal.name;
self.extra.text = [randomAnimal breed];
} completion:^(BOOL finished) {
}];
sender.title = #"And Another";
self.whichDog = randomIndex;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
For some reason in Animal.h I keep getting that error which says "Property with 'retain (or strong)' attribute must be of object type". I am not very sure on what this retain or strong means in the properties, but can someone please explain to me what I am doing wrong in my code. Thank you so much for the help.
UIImage belongs to UIKit, so import UIKit instead of Foundation

Not Properly Passing Data Between Views

I'm trying to send text generated by the user from the AuthenticationViewController to MainProfileViewController. I'm not receiving an error report. The label just doesn't even appear in the MainProfileViewController. The outlets are correctly hooked up. Thanks for the help!
#import <UIKit/UIKit.h>
#class MainProfileViewController;
#interface AuthenticationViewController : UIViewController
{
MainProfileViewController *mainProfileViewController;
}
#property (retain, nonatomic) IBOutlet UITextField *usernameTextField;
#end
#import "AuthenticationViewController.h"
#import "MainProfileViewController.h"
#interface AuthenticationViewController ()
#end
#implementation AuthenticationViewController
#synthesize usernameTextField;
- (IBAction)createAccount: (id)sender {
{
mainProfileViewController = [[MainProfileViewController alloc] init];
mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mainProfileViewController.userText.text = usernameTextField.text;
[self presentViewController:mainProfileViewController animated:YES completion:nil];
}
}
#end
#import <UIKit/UIKit.h>
#import "AuthenticationViewController.h"
#interface MainProfileViewController : UIViewController
{
UITextField *userText;
}
#property (retain, nonatomic) IBOutlet UILabel *resultLabel;
#property (retain, nonatomic) IBOutlet UITextField *userText;
#property (retain, nonatomic) NSString *textPass;
#end
#import "MainProfileViewController.h"
#import "AuthenticationViewController.h"
#interface MainProfileViewController ()
#end
#implementation MainProfileViewController
#synthesize resultLabel, userText, textPass;
- (void)viewDidLoad
{
userText.text = textPass;
[super viewDidLoad];
}
#end
OK, there's a few things you're doing wrong.
First off, you're instantiating a local MainProfileViewController instead of instantiating the one that you have an ivar pointing to.
The second thing that you're doing wrong is trying to send over the view from the AuthenticationViewController to the MainProfileViewController. You shouldn't do that. Instead, pass the text itself; otherwise you are just overwriting pointers, and nothing will show up.
- (IBAction)createAccount: (id)sender {
// DON'T CREATE A LOCAL VARIABLE
// MainProfileViewController *controller = [[MainProfileViewController alloc] initWithNibName:#"MainProfileViewController" bundle:nil];
mainProfileViewController = [[MainProfileViewController alloc] initWithNibName:#"MainProfileViewController" bundle:nil];
mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// DON'T TRY SENDING THE VIEW OVER
// mainProfileViewController.userText = usernameTextField;
mainProfileViewController.userText.text = usernameTextField.text;
[self presentViewController:mainProfileViewController animated:YES completion:nil];
}
edit: I suppose it's not required that you have an ivar to the new view controller you want to present... But the point is that you cannot instantiate one, and then set the parameter on the one you did not instantiate (it will be nil).
Check your story board and see if you connected the Outlets properly.
There should be an IBOutlet object in your view controller that you should connect in story board.
Also check if you connected the correct view controller class to the view controller in storyboard
#import <UIKit/UIKit.h>
#class MainProfileViewController;
#interface AuthenticationViewController : UIViewController
{
MainProfileViewController *mainProfileViewController;
}
#property (retain, nonatomic) IBOutlet UITextField *usernameTextField;
#end
#import "AuthenticationViewController.h"
#import "MainProfileViewController.h"
#interface AuthenticationViewController ()
#end
#implementation AuthenticationViewController
#synthesize usernameTextField;
- (IBAction)createAccount: (id)sender {
{
MainProfileViewController *controller = [[MainProfileViewController alloc] initWithNibName:#"MainProfileViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mainProfileViewController.textpass = usernameTextField.text;
[self presentViewController:controller animated:YES completion:nil];
}
}
#end
#import <UIKit/UIKit.h>
#import "AuthenticationViewController.h"
#interface MainProfileViewController : UIViewController
{
UITextField *userText;
}
#property (retain, nonatomic) IBOutlet UILabel *resultLabel;
#property (retain, nonatomic) IBOutlet UITextField *userText;
#property (retain, nonatomic) NSString *textpass;
#end
#import "MainProfileViewController.h"
#import "AuthenticationViewController.h"
#interface MainProfileViewController ()
#end
#implementation MainProfileViewController
#synthesize resultLabel, userText;
- (void)viewDidLoad {
usertext.text=textpass;
}
#end
You need to change your code on this part...
- (IBAction)createAccount: (id)sender
{
mainProfileViewController = [[MainProfileViewController alloc] init];
mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mainProfileViewController.userText.text = usernameTextField.text;
[self presentViewController:mainProfileViewController animated:YES completion:nil];
}
Happy Coding...

I have two error : No visible #interface for 'UIWebview'

I have two error :No visible #interface for 'UIWebView' declares the selector 'highlightAllOccurencesOfString:'
another one:No visible #interface for 'UIWebView' declares the selector 'removeAllHighlights'
Please someone help me.
WBSecondViewController.h
#import <UIKit/UIKit.h>
#interface WBSecondViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate>{
}
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property(copy) NSArray *menuItems;
#property (weak, nonatomic) IBOutlet UIToolbar *webToolBar;
- (IBAction)back:(id)sender;
- (IBAction)foward:(id)sender;
-(IBAction)searchButtonPressed:(id)sender;
-(IBAction)clearHighlights:(id)sender;
#end
WBSecondViewController.m
#import "WBSecondViewController.h"
#import "Word.h"
#import "WordController.h"
#import "AddWordController.h"
#import "WBAppDelegate.h"
#import "WBFirstViewController.h"
#import "SearchWebView.h"
#interface WBSecondViewController ()
#end
#implementation WBSecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Second", #"Second");
self.tabBarItem.image = [UIImage imageNamed:#"second"];
}
return self;
}
- (void)viewDidLoad
{
UIMenuController *menu = [UIMenuController sharedMenuController];
[super viewDidLoad];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.co.jp"];
[_webView loadRequest:[NSURLRequest requestWithURL:theURL]];
}
-(IBAction)searchButtonPressed:(id)sender{
[_webView highlightAllOccurencesOfString:#"cat"];
}
-(IBAction)clearHighlights:(id)sender{
[_webView removeAllHighlights];
}
SearchWebView.h
#import <Foundation/Foundation.h>
#interface SearchWebView : UIWebView
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;
#end
SearchWebView.m
#import "SearchWebView.h"
#implementation SearchWebView
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"UIWebViewSearch" ofType:#"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self stringByEvaluatingJavaScriptFromString:jsCode];
NSString *startSearch = [NSString stringWithFormat:#"uiWebview_HighlightAllOccurencesOfString('%#')",str];
[self stringByEvaluatingJavaScriptFromString:startSearch];
NSString *result = [self stringByEvaluatingJavaScriptFromString:#"uiWebview_SearchResultCount"];
return [result integerValue];
}
- (void)removeAllHighlights
{
[self stringByEvaluatingJavaScriptFromString:#"uiWebview_RemoveAllHighlights()"];
}
#end
U have subclassed uiwebview and called it SearchWebView but then when you create an instance of web view in your wbsecondviewcontroller, you use a regular web view instead of the subclass that you created and the regular web view does not have the two extra methods that you defined for that custom one. Above #interface in the wbsecondviewcontroller.h do #class SearchWebView. Then where you declare the property UiWebView, declare it as a SearchWebView instead. In the .m file of the wbsecondviewcontroller do #import "SearchWebView.h"

NSString being set but appears empty when calling from another class - iOS

I have a small piece of code that sets the NSString (strUsername) of the class Username to the value which is typed into a UITextField and then a button is pressed. This works fine:
UsernameViewController.m
#import <UIKit/UIKit.h>
#import "Username.h"
#interface UsernameViewController : UIViewController
{
Username *username;
__weak IBOutlet UITextField *test;
}
#property (nonatomic, retain)IBOutlet UITextField *txtUsername;
#property (nonatomic, retain) IBOutlet UITextField *test;
-(IBAction)setUsername;
#end
#import "UsernameViewController.h"
#import "Username.h"
#implementation UsernameViewController
#synthesize test=_test;
- (void)viewDidLoad
{
[super viewDidLoad];
username = [[Username alloc] init];
}
-(IBAction)setUsername
{
NSString *inputUsername = self.test.text;
NSLog(#"inputUsername is: %#", inputUsername);
username.strUsername = inputUsername;
NSLog(#"the username.strUsername is now: %#", username.strUsername);
}
My NSLog output shows that the UItextfield input and NSString setter are working:
LocNews1[6699:707] inputUsername is: Harry brown
LocNews1[6699:707] the username.strUsername is now: Harry brown
Now when it hit the back button on this view it return me back to a UITableViewController. I then perform a pull down to refresh action and its called the following method:
TestViewController.m (UITableViewController type)
#import "TestViewController.h"
#import "ViewController.h"
#import "DetailViewController.h"
#import "AppDelegate.h"
#import "NewsArticle.h"
#import "ResultsCustomCell.h"
#import "XMLParser.h"
#implementation TestViewController
//some code
- (void)addItem
{
username = [[Username alloc] init];
// Use XMLparser to check for updated new feeds.
if(username.strUsername != NULL)
{
NSLog(#"ViewController username.strUsername is:%#",username.strUsername);
activeUsername = username.strUsername;
}
else
{
NSLog(#"%#",username.strUsername);
activeUsername = #"";
}
NSString *myLat = [[NSString alloc] initWithFormat:#"%f", locationManager.location.coordinate.latitude];
NSString *mylong = [[NSString alloc] initWithFormat:#"%f", locationManager.location.coordinate.longitude];
XMLParser *parseQuestionnaire = [[XMLParser alloc] init];
NSLog(#"username %#",activeUsername);
newsArticle = [parseQuestionnaire parseXML:myLat:mylong:activeUsername];
[self.tableView reloadData];
[self stopLoading];
}
However this shows the NSLog output as:
LocNews1[6699:707] ViewController username.strUsername is:
As you can see the string has been set in UsernameViewController.m but when I try and call the string back in TestViewController.m is appears to be blank (there is no null in the NSLog, just blank);
What could be causing this?
EDIT: Username.h/m
#import <Foundation/Foundation.h>
#interface Username : NSObject
{
NSString *strUsername;
}
#property (nonatomic, retain) NSString *strUsername;
#end
#import "Username.h"
#implementation Username
#synthesize strUsername;
-(id)init
{
strUsername = [[NSString alloc] init];
return self;
}
#end
Note: Username is declared in both TestViewController.h and UsernameViewController.h like: Username *username;
The username instance variable in that instance of UsernameViewController is completely unrelated to the username instance variable in the instance of TestViewController. You'll need to store the variable in a place that both controllers know about or pass it between them if you want them both to have access. Simply having two variables with the same name doesn't connect them in any way.