I'm trying to access to another class from my viewcontoller but is not working:
viewcontroller.h
#import <UIKit/UIKit.h>
#class firstClass; //nsobject class
#interface ViewController : UIViewController
{
firstClass *firstclass;
}
#property (retain,nonatomic) LEMZfirstClass *firstclass;
---
firstClass.h:
#import "LEMZViewController.h"
#interface firstClass : NSObject
{
ViewController *viewController;
}
#property (retain,nonatomic) ViewController *viewController;
-(void)doSomenthing;
firstClass.m:
#synthesize viewController;
-(void)doSomenthing
{
viewController.firstclass=self;
viewController.outPutLabel.text=#"This is my Label";
}
viewcontroller.m:
#synthesize firstclass;
- (void)viewDidLoad
{
[super viewDidLoad];
[firstclass doSomenthing];
}
it compiles with no errors but the label is never updated and for that matter the first class is never call it all. What I'm doing wrong? I'll really appreciate your help.
A few things I'm noticing:
Generally you would have the ViewController class handle updating its own UI elements, not another class.
Where is your outPutLabel variable? Is it created by code or an IBOutlet that is wired up in InterfaceBuilder?
Before you can call something on firstclass, you must create it. Something like this:
firstclass = [[firstClass alloc] init];
[firstclass doSomenthing];
The viewController.firstclass=self; line would be redundant then.
Your firstClass.h
#import <Foundation/Foundation.h>
#interface firstClass : NSObject
+(NSString *)doSomenthing; //Instance Class
#end
firstClass.m
#import "firstClass.h"
#implementation firstClass
+(NSString *)doSomenthing
{
return #"This is my Label";
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "firstClass.h"
#interface ViewController : UIViewController
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[firstClass doSomenthing];
outPutLabel.text=[firstClass doSomenthing];;
// Do any additional setup after loading the view, typically from a nib.
}
Note: Here I am using instance class. Before you work this code you must study about Instance class.
Related
I have created some method in category file. I just want to reuse those methods in my view controllers. So I have imported that category file in view controllers and declared the method in header file also.
calling it like this:
Category class:
#interface UIViewController (headerView)
-(UILabel *)someMethod;
#implementation UIViewController (headerView)
-(UILabel *)someMethod{
}
HomeViewController:
#interface HomeViewController : UIViewController
-(UILabel *)someMethod;
#implementation HomeViewController
[self someMethod];
I am getting warning message in this line:
#implementation HomeViewController
It's working. But I want to clear this warning. How can I do it?
If you want you category in view controller do it something like that
Your category
#interface UIViewController (ExtendedMethods)
- (void)someMethod;
#end
#implementation UIViewController (ExtendedMethods)
- (void)someMethod {
NSLog(#"Some method");
}
#end
MyViewController.m
#import "MyViewController.h"
#import "UIViewController+ExtendedMethods.h"
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self someMethod];
}
#end
I need to pass a string from a NSObject class to a UIViewController, I understand that the best way is delegation but the delegate method isn't being called. I'm trying to set the UILabel an DieFacesViewController as the selectedOption from TemporarySelection.
A tableview shows the value of CustomOptionStore, once it's tapped passes its value to TemporarySelection and opens the modal view DieFacesViewCountroller which should, at least in my mind, take the label value from TemporarySelection. The reason I created TemporarySelection is because the DieFacesViewController will be used by other classes, not only by CustomOptionStore, and it will need to load the label from all those classes when different tableViews are selected.
I tried to set the delegate as self in both viewDidLoad and viewWillAppear with no luck, I don't understand if the view loads before being able to call the delegate method or if there's something wrong the way I set the method up.
I've been stuck here for two days, this is the first time I post a question so please forgive me if it's a bit confused.
my delegator class TemporarySelection.h is
#import <Foundation/Foundation.h>
#import "CustomOptionsStore.h"
#class DieFacesViewController;
#protocol TemporarySelectionDelegate <NSObject>
-(void)sendSelection;
#end
#interface TemporarySelection : NSObject
#property (nonatomic, weak) id <TemporarySelectionDelegate> delegate;
#property (nonatomic, strong) NSString *selectedOption;
-(void)addSelection: (CustomOptionsStore *) selection;
#end
and my TemporarySelection.m is
#import "TemporarySelection.h"
#implementation TemporarySelection
-(void)addSelection: (CustomOptionsStore *) selection{
self.selectedOption = selection.description;
[self.delegate sendSelection];
}
#end
the delegate class DiewFacesViewController.h is
#import <UIKit/UIKit.h>
#import "SelectedStore.h"
#import "TemporarySelection.h"
#interface DieFacesViewController : UIViewController <TemporarySelectionDelegate>
#property (strong, nonatomic) IBOutlet UILabel *SelectionName;
#end
and the DieFacesViewController.m is
#import "DieFacesViewController.h"
#interface DieFacesViewController ()
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
TemporarySelection *ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
TemporarySelection *ts = [[TemporarySelection alloc]init];
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}
You are not setting the delegate object properly.Check the above code
#import "DieFacesViewController.h"
#interface DieFacesViewController ()<TemporarySelectionDelegate>
{
//global object
TemporarySelection *ts;
}
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
//Use the object to extract
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}
I have this delegate that is not working out as planned, I have it setup like so. I want to call the function NSLog(show); I am not too sure why this isn't work, but works with my other viewcontroller. I must be missing some small detail.
AccountViewController.h
#protocol AccountViewControllerDelegate;
#interface AccountViewController : UIViewController{
}
#property (nonatomic, assign) id <AccountViewControllerDelegate> accountViewDelegate;
#end
#protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
#end
AccountViewController.m
-(IBAction)save:(id)sender {
[self showLabel];
}
- (void)showLabel {
if (self.accountViewDelegate) {
NSLog(#"showlabel");
[self.accountViewDelegate showLabel];
}
}
MapViewController.m
-(void)showLabel {
NSLog(#"SHOW");
}
you haven't shown where you assign the MapViewController to be the delegate of the AccountViewController. Perhaps thats what you are missing
//(from somewhere in the MapViewController)
AccountViewController *accountVC = //however you instantiate it (segue, storyboard etc
accountVC.accountViewDelegate = self;
Kindly note that delegate should not have strong reference.
So use
#property (unsafe_unretained) id <AccountViewControllerDelegate> accountViewDelegate;
Now in MapViewController.m or MapViewController.h conform to this protocol as
#interface MapViewController : UIViewController <AccountViewControllerDelegate>{
}
Then in MapViewController.m please do
AccountViewController *accountVC = [[AccountViewController alloc]init]; // initialize it with whatever be like storyboard or nib
accountVC.accountViewDelegate = self;
Your AccountViewController.h file should look like this
#protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
#end
#interface AccountViewController : UIViewController{
}
#property (unsafe_unretained) id <AccountViewControllerDelegate> accountViewDelegate;
#end
Here what you should have in your different files:
AccountViewController.h
Note that "assign" is mainly for primitives like NSInteger, CGFloat, BOOL (that is, for properties that are not objects). Use "weak" to keep a pointer toward your delegate without incrementing his retain count.
#protocol AccountViewControllerDelegate;
#interface AccountViewController : UIViewController
#property (nonatomic, weak) id <AccountViewControllerDelegate> accountViewDelegate;
#end
#protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
#end
AccountViewController.m
Here, it is usually good practice to check if the delegate isn't nil and if it has the method you want to call implemented. Use respondToSelector for that.
-(IBAction)save:(id)sender {
[self showLabel];
}
- (void)showLabel {
if (self.accountViewDelegate && [self.accountViewDelegate respondsToSelector:#selector(showLabel)]) {
NSLog(#"showlabel");
[self.accountViewDelegate showLabel];
}
}
MapViewController.h
Be sure to import "AccountViewController.h" here.
#import "AccountViewController.h"
#interface MapViewController : UIViewController <AccountViewControllerDelegate>
#end
MapViewController.m
Put the following where you were instantiating your AccountViewController object:
//Somewhere
AccountViewController *accountViewController = [[AccountViewController alloc] init];
accountViewController.accountViewDelegate = self;
and this is your delegate's method implementation:
- (void)showLabel {
NSLog(#"SHOW");
}
I was using MapView on Xcode, and everything was working fine, but when I added the following line
mapView.delegate = self;
to ViewController.m, I get the error
Assigning to 'id<MKMapViewDelegate>' from incompatible type 'ViewController *const
__strong'
Here is my code:
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.showsUserLocation = YES;
mapView.delegate = self; //The line I added that creates the error
// Do any additional setup after loading the view, typically from a nib.
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController {
MKMapView *mapview;
}
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
You need to declare that your class implements the MKMapViewDelegate methods. In your ViewController.h header file, change the line
#interface ViewController : UIViewController {
to
#interface ViewController : UIViewController <MKMapViewDelegate> {
You need to declare that you respond to MKMapViewDelegate calls.
To do this, simply update the header file for the corresponding class (ViewController.h in your example) as follows:
#interface ViewController : UIViewController <MKMapViewDelegate> {
I am trying to call a method in my data controller object to load the data for my application, but for some reason it is not being called. Below is what I have done to initialize it. Any help would be greatly appreciated.
ViewController:
header file:
#import <UIKit/UIKit.h>
#class DetailViewController;
#class DataController;
#import <CoreData/CoreData.h>
#import "JointCAD.h"
#interface TableViewController : UITableViewController {
}
#property (nonatomic, assign) DataController *dataController;
#end
implementation file:
#import "TableViewController.h"
#import "DataController.h"
#implementation TableViewController
#synthesize dataController;
- (void)viewDidLoad {
[dataController refreshData];
}
#end
Data Controller:
header file:
#import <Foundation/Foundation.h>
#import "JointCAD.h"
#import "JointCADXMLParser.h"
#import "TFHpple.h"
#interface DataController : NSObject {
TFHpple *xpathParser;
}
- (void)refreshData;
- (void)initXMLParser;
- (void)noCallsMessage;
- (void)noInternetMessage;
#end
implementation file:
#import "DataController.h"
#implementation DataController
XMLParser *xmlParser;
- (void)refreshData {
NSLog("Some Method");
}
Is 'dataController' Object being set by some other class? - I believe that's why you have set it as a property? Right?
If No, then Remove the property,#synthesize of 'dataController' and try simple allocation of your 'dataController' object and then try calling your method.
Hope it helps.
You either need to initialize "DataController" prior to actually calling one of it's methods, or you need to make the method, "refreshData" a class by changing it's "-" to a "+".
If you need an instance callback instead. You need to rewrite "viewDidLoad" like this:
- (void)viewDidLoad {
DataController *dataController = [[DataController alloc] init];
[dataController refreshData];
}
And get rid of the property declaration of dataController because you haven't initialized it. If you would prefer a property declaration instead, simply allocate the viewcontroller prior to calling a function from it.
- (void)viewDidLoad {
dataController = [[DataController alloc] init];
[dataController refreshData];
}
One last thing to note is that I (and probably Ray) assume that you're using a storyboard configuration. If you are using a xib configuration, you need to add initWithNibName: to each initialization of the view controller.
I hope that's helpful!