accessing method of custom view in view controller - objective-c

I am a new-be in Objective C. Here I want to ask a simple question.
I have created a view controller in storyboard, and customized its view with a subclass of UIView.
However, I don't know how to call methods of the view in my view controller. Can anyone help? All I want to do is to call drawLine:pointStore in ChartViewController.m from chartView.m
Here are some of my codes.
ChartViewController.h
#import <UIKit/UIKit.h>
#class chartView;
#interface ChartViewController : UIViewController{
chartView *chart_view;
}
ChartViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableDictionary *pointStore = [[NSMutableDictionary alloc]init];
NSNumber *initX;
NSNumber *initY;
NSMutableDictionary *variableSet = [[NSMutableDictionary alloc]init];
for(initX = [NSNumber numberWithDouble:-10.0f];initX.floatValue<=10.0f;initX = [NSNumber numberWithDouble:(initX.floatValue+0.5f)] )
{
[variableSet setValue:initX forKey:#"x"];
initY = [NSNumber numberWithDouble:[self.brain performOperation:equationOfChart withVariable:variableSet]];
[pointStore setObject:initX forKey:initY];
}
[chart_view drawLine:pointStore];
}
chartView.h
#interface chartView : UIView
#property (nonatomic, strong) NSString *equation;
-(void) getEquation:(NSString *)Equation;
-(void) drawLine:(NSMutableDictionary *)pointsStore;
#end
chartView.m
-(void)drawLine:(NSMutableDictionary *)pointsStore{
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextStrokePath(c);
for(NSNumber *ax = [NSNumber numberWithDouble:-10.0f];ax.floatValue<10.0f;){
float ay = [[pointsStore objectForKey:ax]doubleValue];
ax = [NSNumber numberWithDouble:(ax.floatValue+0.5f)];
int by = [[pointsStore objectForKey:ax]doubleValue];
[self.class line:ax.floatValue y:ay x2:(ax.floatValue+0.5) y2:by];
}
}

ChartViewController.h
#import <UIKit/UIKit.h>
#class chartView;
#interface ChartViewController : UIViewController
#property (string, nonatomic) chartView *chart_view;
#end
chartView.m
- (void)viewDidLoad
{
[super viewDidLoad];
...
// Don't forget to alloc and init
self.chart_view = [[chartView alloc] init];
[self.chart_view drawLine:pointStore];
}

You should check that the instance of chart_view is created and assigned. Probably you have nil in that poiner.
How to do this -- depend on the way you have chosen to create the view: in Interface Builder or dynamically, in code.

Related

pass array from NSViewController to NSView in Objective C Cocoa

Im making a program to draw the chart (which i keep in NSView class). However action & data I want to pass from NSViewController. So could you help me how to do it. I did try as bellow code however it doesn't work.
#implementation PlottingChart
#synthesize plotChartData;
- (void)drawRect:(NSRect)dirtyRect{
[super drawRect:dirtyRect];
[self drawChartGrid:plotChartData];
}
-(void)drawChartGrid:(NSMutableArray *)ChartData
{
//Drawing code here
}
#interface PlottingChart : NSView
#property (nonatomic, strong) NSMutableArray *plotChartData;
-(void)drawChartGrid:(NSMutableArray *)ChartData;
#end
#import "PlottingChart.h"
#interface ViewController :NSViewController<NSTableViewDataSource,NSTableViewDelegate>
{
PlottingChart *boxPlotChart;
}
- (IBAction)btnStart:(id)sender {
//trial draw chart
NSDictionary *dict1 = #{#"plot_Q1":#"180",#"plot_Q3": #"220", #"plot_Max":#"250", #"plot_Min":#"150", #"plot_Median":#"200"};
NSDictionary *dict2 = #{#"plot_Q1":#"190",#"plot_Q3": #"230", #"plot_Max":#"280", #"plot_Min":#"160", #"plot_Median":#"210"};
NSMutableArray *array = [NSMutableArray arrayWithObjects:dict1,dict2, nil];
boxPlotChart.plotChartData = array;
[boxPlotChart drawChartGrid:array];
boxPlotChart = [[PlottingChart alloc] initWithFrame:NSMakeRect(10, -50, 850, 360) ]; // x,y lenght,height
[self.view addSubview:boxPlotChart];
}
Finally I found the solution.
Add initWithFrame method into NSView.
- (id)initWithFrame:(NSRect)frame dataArray:(NSMutableArray *)dtArray;
{
self = [super initWithFrame:frame];
self.plotChartData = dtArray;
return self;
}
And initial this one in NSViewController.
boxPlotChart = [[PlottingChart alloc] initWithFrame:NSMakeRect(10, -50, 850, 360) dataArray:array ];

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

Cannot read class members values in Objective C

I'm trying to learn Objective C with examples, and now I stuck with the following problem. I have a code:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *mapView;
MKPointAnnotation* startPoint;
MKPointAnnotation* endPoint;
double startPointLat;
double startPointLon;
double endPointLat;
double endPointLon;
}
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
- (IBAction)findPressed:(id)sender;
#end
And implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
// Add an annotation
startPoint = [[MKPointAnnotation alloc] init];
startPoint.coordinate = CLLocationCoordinate2DMake(0, 0);
startPoint.title = #"Start point";
endPoint = [[MKPointAnnotation alloc] init];
endPoint.coordinate = CLLocationCoordinate2DMake(10, 10);
endPoint.title = #"End point";
startPointLat = 0;
startPointLon = 0;
endPointLat = 10;
endPointLon = 10;
[self.mapView addAnnotation:startPoint];
[self.mapView addAnnotation:endPoint];
NSLog(#"%#, %# -> %#, %#", startPointLat, self->startPointLon, self->endPointLat, self->endPointLon);
}
But values in logs output are nulls. What's wrong?
The format specifier "%#" is for objects, but the variables here are doubles. You want "%f" instead.

Calling SetNeedsDisplay from other class

Today I'm facing a new problem:
I have a ViewController (Subclass of NSView) and another class (Subclass of NSObject) which, through an IBAction, try to call back the viewController to redraw its view using SetNeedsDisplay:YES.
The method to redraw the view (ViewController.m) is:
- (void) redrawView {
[self setNeedsDisplay:YES]
}
// With an NSLog i see that the method is called !
// Instead of self i tried even an outlet connected to the custom view.
}
What I'm doing to call the ViewController method through my other class is:
1) #import "ViewController.h"
2) into the IBAction i made a new istance of ViewController as:
ViewController *newIstanceOfViewController = [[ViewController alloc] init]
3) [newIstanceOfViewController redrawView]
The log show me that setNeedsDisplay is called but drawrect no ! Why ? I have forgotten to init or subview something ? Thanks
THE ORIGINAL CODE HERE (DIFFERENT NAME OF METHODS DUE TO LANGUAGE, SAME SYNTAX)
// Controller.h
#import <Cocoa/Cocoa.h>
#interface Controller : NSView {
IBOutlet NSView *tastierinoView;
}
- (void) aggiornaTastierinoView;
#end
// Controller.m
#import "Controller.h"
#implementation Controller
//Contatore numero volte disegno view
int contatore = 0;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
tastierinoView = [[NSView alloc] init];
}
return self;
}
- (void) aggiornaTastierinoView { //THIS IS THE METHOD TO CALL REDRAW
[tastierinoView setNeedsDisplay:YES];
NSLog(#"Chiamo setneedsDisplay\n\n");
}
- (void)drawRect:(NSRect)dirtyRect
{
contatore++;
NSLog(#"La view รจ stata disegnata %d volte\n\n",contatore);
[super drawRect:dirtyRect];
// Drawing code here.
NSBezierPath* percorso = [NSBezierPath bezierPath];
[[NSColor cyanColor] set];
[percorso moveToPoint:NSMakePoint(150, 150)];
[percorso lineToPoint:NSMakePoint(250, 150)];
[percorso setLineWidth:5.0];
[percorso stroke];
}
#end
// ManipolatorePin.h
#import <Foundation/Foundation.h>
#interface ManipolatorePin : NSObject {
IBOutlet NSWindow *finestraPrincipaleWindow;
IBOutlet NSTextField *codiceTextField;
NSArray *coordinate_x;
NSArray *coordinate_y;
//L'array sotto riportato serve, tramite un ciclo for, a salvare il codice pin spezzato in singoli numeri che corrisponderanno al punto.
NSMutableArray *numeroAllaIndexArray;
}
- (IBAction)aggiornaTastierino:(id)sender;
#end
// ManipolatorePin.m
#import "ManipolatorePin.h"
#import "Controller.h"
#implementation ManipolatorePin
- (void)awakeFromNib {
coordinate_x = [[NSArray alloc] initWithObjects:#"150",#"50",#"150",#"250",#"50",#"150",#"250",#"50",#"150",#"250", nil];
coordinate_y = [[NSArray alloc] initWithObjects:#"50",#"150",#"150",#"150",#"250",#"250",#"250",#"350",#"350",#"350", nil];
numeroAllaIndexArray = [[NSMutableArray alloc] init];
NSLog(#"Array coordinate iniziallizato.\nTest:(%#) -> deve risultare \"50\"\n\n",[coordinate_x objectAtIndex:4]);
}
- (IBAction)aggiornaTastierino:(id)sender {
NSString *codiceString = [[NSString alloc] initWithFormat:[NSString stringWithFormat:#"%#",[codiceTextField stringValue]]];
NSLog(#"codiceTextField = %#", codiceString);
int lunghezzaCodiceString;
NSLog(#"Il codice risulta essere composto da (%d) numeri\n\n",[codiceString length]);
//Svuoto array
[numeroAllaIndexArray removeAllObjects];
for (lunghezzaCodiceString = 0; lunghezzaCodiceString < [codiceString length]; lunghezzaCodiceString++) {
//Compilo array (Ci provo ahah)
NSString *carattereDelCodiceStringInEsame = [[NSString alloc] init];
carattereDelCodiceStringInEsame = [codiceString substringWithRange:NSMakeRange(lunghezzaCodiceString,1)];
NSLog(#"Aggiungo il numero (%#) all'array 'numeroAllaIndexArray'",carattereDelCodiceStringInEsame);
[numeroAllaIndexArray addObject:carattereDelCodiceStringInEsame];
}
//DEBUG - DA QUI IN POI E' CANCELLABILE
NSLog(#"\n\nCiclo for termitato\nProcesso concluso con successo\n\n\nContenuto array:");
int conteggioArray;
for (conteggioArray = 0; conteggioArray < [numeroAllaIndexArray count] ; conteggioArray++ ) {
NSLog(#"index (%d) -> (%#)",conteggioArray,[numeroAllaIndexArray objectAtIndex:conteggioArray]);
NSLog(#"\n\n");
}
//FINE DEBUG
///////////////HERE THE INSTANCE TO CALL THE CONTROLLER
Controller *istanzaGestionaleController = [[Controller alloc] init];
[istanzaGestionaleController aggiornaTastierinoView];
}
#end
It sounds like you have two unrelated instances of ViewController. You need to call redrawView on the instance that actually has a view on the screen.
In your class (which is not the NSViewController), create a property like this:
#property (nonatomic, readwrite, weak) NSViewController *vc;
When the class is instantiated:
YourClass *yourInstance = [[YourClass alloc] init];
assign the value of vc:
yourInstance.vc = self; // self is your NSViewController instance
Then when you want to trigger a redraw, you pass the command to self.vc inside your other class.

Why is MKMapView delegate method not being called?

I'm very new to iphone development and I'm trying to annotate a map. I've been able to get 3 location points on a map with hardcoded coordinates and now I'm trying to customize the pins. I know to do that you need to implement the:-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation { delegate method.
For some reason I've been unable to even get it called. Here is what I have in the controller header:
#import "ArtPiece.h"
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface TestViewController : UIViewController <MKMapViewDelegate> {
MKMapView *mapView;
NSMutableArray *mapAnnotations;
float results_lat[3];
float results_long[3];
NSMutableArray *results_title;
NSMutableArray *Arts;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
#property (nonatomic, retain) NSMutableArray *mapAnnotations;
#end
And here is what I have in the controller implementation:
- (void)viewDidLoad {
results_long[0] = -122.477989;
results_lat[0] = 37.810000;
results_long[1] = -122.480000;
results_lat[1] = 37.820000;
results_long[2] = -122.4850000;
results_lat[2] = 37.830000;
self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:3];
Arts = [[NSMutableArray alloc] initWithCapacity:3];
for(int x = 0; x<3; x++)
{
// creates an ArtPiece object and sets its lat and long
ArtPiece *a = [[ArtPiece alloc] init];
a.longitude = [NSNumber numberWithDouble:results_long[x]];
a.latitude = [NSNumber numberWithDouble:results_lat[x]];
a.title = #"please show up";
// add objects to annotation array
[self.mapAnnotations insertObject:a atIndex:x];
[a release];
}
// center screen on cali area
[self gotoLocation];
for(int x = 0; x<3; x++)
{
[mapView addAnnotations:mapAnnotations];
}
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
NSLog(#"This is not printing to to console...");
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
And here is the ArtPiece class implementation:
#import "TestViewController.h"
#import "ArtPiece.h"
#import <MapKit/MapKit.h>
#import <CoreData/CoreData.h>
#implementation ArtPiece
#synthesize title, artist, series, description, latitude, longitude;
- (CLLocationCoordinate2D)coordinate
{
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = [self.latitude doubleValue];
theCoordinate.longitude = [self.longitude doubleValue];
return theCoordinate;
}
#end
It's probably something simple. Is there something wrong with my method declaration? Did I declare the delegate wrong? I can't figure it out, again, I'm very new to this objective c/delegate stuff. Thanks for any help.
The addAnnotations method expects an NSArray of objects that conform to the MKAnnotation protocol.
You are adding objects of type ArtPiece which don't seem to implement MKAnnotation which has a coordinate property (not latitude and longitude properties).
Update your ArtPiece class to conform to MKAnnotation (or use the pre-defined MKPointAnnotation class). But updating your custom class is a better fix.