Access variable in once from another class in Objective-C - 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);
}

Related

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

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

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...

Content Not Moving Between Classes?

I am writing an app that is transferring data from one class to another. For some reason the text is not being displayed on the other view when it is supposed to.
My code:
.h Master class:
#import <UIKit/UIKit.h>
#class SSWSettings;
#interface SSWSelectProgram : UITableViewController
#property (strong, nonatomic) SSWSettings *detailViewController;
#property (weak, nonatomic) IBOutlet UILabel *p1;
#end
.m master class:
#import "SSWSelectProgram.h"
#import "SSWSettings.h"
#interface SSWSelectProgram ()
#end
#implementation SSWSelectProgram
#synthesize p1;
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cellChosen = [self.tableView cellForRowAtIndexPath:indexPath];
if (cellChosen == static1) {
self.detailViewController.detailItem = p1.text;
[self dismissViewControllerAnimated:YES completion:NULL];
}
.h detail class
#import <UIKit/UIKit.h>
#interface SSWSettings : UITableViewController <UITableViewDelegate, UITableViewDataSource>
#property (strong, nonatomic) id detailItem;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#end
.m detail class
#import "SSWSettings.h"
#import "SSWSelectProgram.h"
#interface SSWSettings ()
-(void)configureView;
#end
#implementation SSWSettings
#synthesize detailDescriptionLabel;
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[self configureView];
}
Your object "detailItem" is of type id.
Thus, it does not inherit from NSObject and the
[self.detailItem description];
call Will not return anything.
Try
self.detailDescriptionLabel.text = (NSString*) self.detailItem;
A better solution, of course, is to not use the type 'id' and use NSString

Mac OS X using WebView object

I can't see the webview when I launch tha app..
In the delegate that's my code
#import <Cocoa/Cocoa.h>
#include "NewsViewController.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic,strong) IBOutlet NSSplitView *splitView;
#property (nonatomic,strong) IBOutlet NewsViewController *newsViewController;
#end
#import "AppDelegate.h"
#implementation AppDelegate
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
// 1. Create the master View Controller
self.newsViewController = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.splitView addSubview:self.newsViewController.view];
}
#end
That's my viewcontroller
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface NewsViewController : NSViewController{
IBOutlet WebView *web;
}
#property (assign) IBOutlet WebView *web;
#end
#import "NewsViewController.h"
#interface NewsViewController ()
#end
#implementation NewsViewController
#synthesize web;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(void)loadView{
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[web mainFrame] loadRequest:requestObj];
}
#end
if I out one label on my view in the controller everything is fine but if I put a webview I see only a grey window.
Why this?
thanks
I do believe you need to call [super loadView] in your loadView implementation.

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"