Make an IBOutlet UIImageView accessible from inside of a methods - iphone

So, I have some IBOutlets set up for a bunch of UIImageView instances like this in my ViewController.h:
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey21;
In my implementation, I can access the IBOutlet when I'm in the viewDidLoad method like this:
[whiteKey21 setImage:image]; //image is declared and instantiated in viewDidLoad
My problem is that when I try to execute the same code in another methods, namely this one:
void noteUserCallback (void *inClientData, MusicSequence inSequence, MusicTrack inTrack, MusicTimeStamp inEventTime, const MusicEventUserData *inEventData, MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
{
UserEvent* event = (UserEvent *)inEventData;
UInt32 size = event->length;
UInt32 note = event->playedNote;
UInt32 timestamp = event->tStamp;
NSLog(#"Size: %lu Note: %lu, Timestamp: %lu", size, note, timestamp);
//Want to change the IBOutlet here.
}
It doesn't recognize the UIImages or UIImageViews that I declared in the header. I have a feeling that it is because this function is C-based due to the fact that that function has to interact with the MusicPlayer API. But I have no idea how to fix it? I tried making an Obj-C method that I could then call within the noteUserCallback function but it didn't recognize that either :(
Any help would be greatly appreciated. Thanks :)
EDIT
Here is the full code for the PracticeViewController.h and PracticeViewController.m
What I want to do is to set the image displayed by the UIImageViews (whiteKey21, etc.) from inside of the noteUserCallback function. The function is passed in some data (a UserEvent) whenever a user event is passed in the playback of the MIDI. This allows me to actively track what notes are being played. What I need to do, is to make a decision based on the "note" data in the UserEvent on which UIImageView to load an image into. But, I can't access the UIImageViews or the UIImages for that matter from inside the function for some reason. I have no problem access them in the other methods.
PracticeViewController.h
#import <UIKit/UIKit.h>
#import "Lesson.h"
#import "Note.h"
#import <AudioToolbox/AudioToolbox.h>
#interface PracticeViewController : UIViewController
#property (strong, nonatomic) Lesson *selectedLesson;
#property (strong, nonatomic) IBOutlet UINavigationItem *practiceWindowTitle;
#property MusicPlayer player;
//Outlets for White Keys
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey21;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey23;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey24;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey26;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey28;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey29;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey31;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey33;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey35;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey36;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey38;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey40;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey41;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey43;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey45;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey47;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey48;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey50;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey52;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey53;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey55;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey57;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey59;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey60;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey62;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey64;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey65;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey67;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey69;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey71;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey72;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey74;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey76;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey77;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey79;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey81;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey83;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey84;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey86;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey88;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey89;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey91;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey93;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey95;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey96;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey98;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey100;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey101;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey103;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey105;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey107;
#property (strong, nonatomic) IBOutlet UIImageView *whiteKey108;
//Outlets for Black Keys
#property (strong, nonatomic) IBOutlet UIImageView *blackKey22;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey25;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey27;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey30;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey32;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey34;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey37;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey39;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey42;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey44;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey46;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey49;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey51;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey54;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey56;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey58;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey61;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey63;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey66;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey68;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey70;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey73;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey75;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey78;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey80;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey82;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey85;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey87;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey90;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey92;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey94;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey97;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey99;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey102;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey104;
#property (strong, nonatomic) IBOutlet UIImageView *blackKey106;
// Key Highlight Images
#property (strong, nonatomic) UIImage *highlightA;
#property (strong, nonatomic) UIImage *highlightB;
#property (strong, nonatomic) UIImage *highlightC;
#property (strong, nonatomic) UIImage *highlightD;
#property (strong, nonatomic) UIImage *highlightE;
#property (strong, nonatomic) UIImage *highlightF;
#property (strong, nonatomic) UIImage *highlightG;
#property (strong, nonatomic) UIImage *highlightH;
- (IBAction)practiceLesson:(id)sender;
#end
PracticeViewController.m
#import "PracticeViewController.h"
#interface PracticeViewController ()
#end
#implementation PracticeViewController
#synthesize blackKey22;
#synthesize blackKey25;
#synthesize blackKey27;
#synthesize blackKey30;
#synthesize blackKey32;
#synthesize blackKey34;
#synthesize blackKey37;
#synthesize blackKey39;
#synthesize blackKey42;
#synthesize blackKey44;
#synthesize blackKey46;
#synthesize blackKey49;
#synthesize blackKey51;
#synthesize blackKey54;
#synthesize blackKey56;
#synthesize blackKey58;
#synthesize blackKey61;
#synthesize blackKey63;
#synthesize blackKey66;
#synthesize blackKey68;
#synthesize blackKey70;
#synthesize blackKey73;
#synthesize blackKey75;
#synthesize blackKey78;
#synthesize blackKey80;
#synthesize blackKey82;
#synthesize blackKey85;
#synthesize blackKey87;
#synthesize blackKey90;
#synthesize blackKey92;
#synthesize blackKey94;
#synthesize blackKey97;
#synthesize blackKey99;
#synthesize blackKey102;
#synthesize blackKey104;
#synthesize blackKey106;
#synthesize whiteKey21;
#synthesize whiteKey23;
#synthesize whiteKey24;
#synthesize whiteKey26;
#synthesize whiteKey28;
#synthesize whiteKey29;
#synthesize whiteKey31;
#synthesize whiteKey33;
#synthesize whiteKey35;
#synthesize whiteKey36;
#synthesize whiteKey38;
#synthesize whiteKey40;
#synthesize whiteKey41;
#synthesize whiteKey43;
#synthesize whiteKey45;
#synthesize whiteKey47;
#synthesize whiteKey48;
#synthesize whiteKey50;
#synthesize whiteKey52;
#synthesize whiteKey53;
#synthesize whiteKey55;
#synthesize whiteKey57;
#synthesize whiteKey59;
#synthesize whiteKey60;
#synthesize whiteKey62;
#synthesize whiteKey64;
#synthesize whiteKey65;
#synthesize whiteKey67;
#synthesize whiteKey69;
#synthesize whiteKey71;
#synthesize whiteKey72;
#synthesize whiteKey74;
#synthesize whiteKey76;
#synthesize whiteKey77;
#synthesize whiteKey79;
#synthesize whiteKey81;
#synthesize whiteKey83;
#synthesize whiteKey84;
#synthesize whiteKey86;
#synthesize whiteKey88;
#synthesize whiteKey89;
#synthesize whiteKey91;
#synthesize whiteKey93;
#synthesize whiteKey95;
#synthesize whiteKey96;
#synthesize whiteKey98;
#synthesize whiteKey100;
#synthesize whiteKey101;
#synthesize whiteKey103;
#synthesize whiteKey105;
#synthesize whiteKey107;
#synthesize whiteKey108;
#synthesize selectedLesson, practiceWindowTitle, player, highlightA, highlightB, highlightC, highlightD, highlightE, highlightF, highlightG, highlightH;
// Implement the UserEvent structure.
typedef struct UserEvent {
UInt32 length;
UInt32 typeID;
UInt32 trackID;
MusicTimeStamp tStamp;
MusicTimeStamp dur;
int playedNote;
} UserEvent;
// Implement the UserCallback function.
void noteUserCallback (void *inClientData, MusicSequence inSequence, MusicTrack inTrack, MusicTimeStamp inEventTime, const MusicEventUserData *inEventData, MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
{
PracticeViewController *pvc = (__bridge PracticeViewController *)inClientData;
UserEvent* event = (UserEvent *)inEventData;
UInt32 size = event->length;
UInt32 note = event->playedNote;
UInt32 timestamp = event->tStamp;
NSLog(#"Size: %lu Note: %lu, Timestamp: %lu", size, note, timestamp);
switch (note) {
case 60:
[pvc.whiteKey21 setImage:pvc.highlightA];
break;
default:
break;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.practiceWindowTitle.title = selectedLesson.titleAndSubtitle;
// Load in the images for the glow.
highlightA = [UIImage imageNamed:#"glow_whiteKeysA.png"];
highlightB = [UIImage imageNamed:#"glow_whiteKeysB.png"];
highlightC = [UIImage imageNamed:#"glow_whiteKeysC.png"];
highlightD = [UIImage imageNamed:#"glow_whiteKeysD.png"];
highlightE = [UIImage imageNamed:#"glow_whiteKeysE.png"];
highlightF = [UIImage imageNamed:#"glow_whiteKeysF.png"];
highlightG = [UIImage imageNamed:#"glow_blackKey.png"];
highlightH = [UIImage imageNamed:#"glow_whiteKeysH.png"];
// Create player, sequence, left/right hand tracks, and iterator.
NewMusicPlayer(&player);
MusicSequence sequence;
NewMusicSequence(&sequence);
MusicTrack rightHand;
MusicTrack leftHand;
MusicEventIterator iterator;
// Load in MIDI file.
NSString *path = [[NSString alloc] init];
path = [[NSBundle mainBundle] pathForResource:selectedLesson.midiFilename ofType:#"mid"];
NSURL *url = [NSURL fileURLWithPath:path];
MusicSequenceFileLoad(sequence, (__bridge CFURLRef)url, 0, kMusicSequenceLoadSMF_ChannelsToTracks);
// Get the right and left hand tracks from the sequence.
int rightHandIndex = 0;
//int leftHandIndex = 1;
MusicSequenceGetIndTrack(sequence, rightHandIndex, &rightHand); //Get right hand.
//MusicSequenceGetIndTrack(sequence, leftHandIndex, leftHand); //Get left hand.
//Iterate through the right hand track and add user events.
Boolean hasNextEvent = false;
Boolean hasEvent = false;
NewMusicEventIterator(rightHand, &iterator);
MusicEventIteratorHasCurrentEvent(iterator, &hasEvent);
MusicEventIteratorHasNextEvent(iterator, &hasNextEvent);
while (hasNextEvent == true) {
MusicTimeStamp timestamp = 0;
MusicEventType eventType = 0;
const void *eventData = NULL;
int note;
MusicTimeStamp duration;
MusicEventIteratorGetEventInfo(iterator, &timestamp, &eventType, &eventData, NULL);
if (eventType == kMusicEventType_MIDINoteMessage) {
MIDINoteMessage *noteMessage = (MIDINoteMessage *)eventData;
note = noteMessage->note;
duration = noteMessage->duration;
UserEvent event;
// Hacky way of doing this...Change one we get the UserEvent class figured out.
event.length = 0;
event.length = sizeof(UserEvent);
event.playedNote = note;
event.tStamp = timestamp;
MusicEventUserData* data = (MusicEventUserData *)&event;
MusicTrackNewUserEvent(rightHand, timestamp, data);
}
MusicEventIteratorHasNextEvent(iterator, &hasNextEvent);
MusicEventIteratorNextEvent(iterator);
}
MusicSequenceSetUserCallback(sequence, noteUserCallback, (__bridge void*)self);
MusicPlayerSetSequence(player, sequence);
MusicPlayerPreroll(player);
}
- (void)viewDidUnload
{
[self setPracticeWindowTitle:nil];
[self setWhiteKey21:nil];
[self setWhiteKey23:nil];
[self setWhiteKey24:nil];
[self setWhiteKey26:nil];
[self setWhiteKey28:nil];
[self setWhiteKey29:nil];
[self setWhiteKey31:nil];
[self setWhiteKey33:nil];
[self setWhiteKey35:nil];
[self setWhiteKey36:nil];
[self setWhiteKey38:nil];
[self setWhiteKey40:nil];
[self setWhiteKey41:nil];
[self setWhiteKey43:nil];
[self setWhiteKey45:nil];
[self setWhiteKey47:nil];
[self setWhiteKey48:nil];
[self setWhiteKey50:nil];
[self setWhiteKey52:nil];
[self setWhiteKey53:nil];
[self setWhiteKey55:nil];
[self setWhiteKey57:nil];
[self setWhiteKey59:nil];
[self setWhiteKey60:nil];
[self setWhiteKey62:nil];
[self setWhiteKey64:nil];
[self setWhiteKey65:nil];
[self setWhiteKey67:nil];
[self setWhiteKey69:nil];
[self setWhiteKey71:nil];
[self setWhiteKey72:nil];
[self setWhiteKey74:nil];
[self setWhiteKey76:nil];
[self setWhiteKey77:nil];
[self setWhiteKey79:nil];
[self setWhiteKey81:nil];
[self setWhiteKey83:nil];
[self setWhiteKey84:nil];
[self setWhiteKey86:nil];
[self setWhiteKey88:nil];
[self setWhiteKey89:nil];
[self setWhiteKey91:nil];
[self setWhiteKey93:nil];
[self setWhiteKey95:nil];
[self setWhiteKey96:nil];
[self setWhiteKey98:nil];
[self setWhiteKey100:nil];
[self setWhiteKey100:nil];
[self setWhiteKey101:nil];
[self setWhiteKey103:nil];
[self setWhiteKey105:nil];
[self setWhiteKey107:nil];
[self setWhiteKey108:nil];
[self setBlackKey22:nil];
[self setBlackKey25:nil];
[self setBlackKey27:nil];
[self setBlackKey30:nil];
[self setBlackKey32:nil];
[self setBlackKey34:nil];
[self setBlackKey37:nil];
[self setBlackKey39:nil];
[self setBlackKey42:nil];
[self setBlackKey44:nil];
[self setBlackKey46:nil];
[self setBlackKey49:nil];
[self setBlackKey51:nil];
[self setBlackKey54:nil];
[self setBlackKey56:nil];
[self setBlackKey58:nil];
[self setBlackKey61:nil];
[self setBlackKey63:nil];
[self setBlackKey66:nil];
[self setBlackKey68:nil];
[self setBlackKey70:nil];
[self setBlackKey73:nil];
[self setBlackKey75:nil];
[self setBlackKey78:nil];
[self setBlackKey80:nil];
[self setBlackKey82:nil];
[self setBlackKey85:nil];
[self setBlackKey87:nil];
[self setBlackKey90:nil];
[self setBlackKey92:nil];
[self setBlackKey94:nil];
[self setBlackKey97:nil];
[self setBlackKey99:nil];
[self setBlackKey102:nil];
[self setBlackKey104:nil];
[self setBlackKey106:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (IBAction)practiceLesson:(id)sender {
MusicPlayerStart(player);
}
#end
EDIT
The code relating to MusicSequenceSetUserCallback. This is declared in the MusicPlayer API (AudioToolbox/MusicPlayer.h):
/*!
#typedef MusicSequenceUserCallback
#discussion See MusicSequenceSetUserCallback
*/
typedef void (*MusicSequenceUserCallback)( void *inClientData,
MusicSequence inSequence,
MusicTrack inTrack,
MusicTimeStamp inEventTime,
const MusicEventUserData *inEventData,
MusicTimeStamp inStartSliceBeat,
MusicTimeStamp inEndSliceBeat);
/*!
#function MusicSequenceSetUserCallback
#abstract Establish a user callback for a sequence
#discussion This call is used to register (or remove if inCallback is NULL) a callback
that the MusicSequence will call for ANY UserEvents that are added to any of the
tracks of the sequence.
If there is a callback registered, then UserEvents will be chased when
MusicPlayerSetTime is called. In that case the inStartSliceBeat and inEndSliceBeat
will both be the same value and will be the beat that the player is chasing too.
In normal cases, where the sequence data is being scheduled for playback, the
following will apply:
inStartSliceBeat <= inEventTime < inEndSliceBeat
The only exception to this is if the track that owns the MusicEvent is looping.
In this case the start beat will still be less than the end beat (so your callback
can still determine that it is playing, and what beats are currently being scheduled),
however, the inEventTime will be the original time-stamped time of the user event.
#param inSequence the sequence
#param inCallback the callback
#param inClientData client (user supplied) data provided back to the callback when it is called by the sequence
*/
extern OSStatus
MusicSequenceSetUserCallback( MusicSequence inSequence,
MusicSequenceUserCallback inCallback,
void* inClientData) __OSX_AVAILABLE_STARTING(__MAC_10_3,__IPHONE_5_0);

In all CoreAudio callbacks you have a "user data" parameter, which is probably in your callback the void *inClientData one, which allows to pass to the callback ANY object the user wants to pass.
In your case when you register the callback, e.g. when you create an Audio Queue Input, you pass in the long list of parameters the name of the callback but you can also pass the address of the user data structure you prefer that will be sent to the callback using the inClientData pointer mentioned before. In your case what you can pass is the address of your class instance that contains the UIImageViews (e.g. if your class instance is "myObj" then you will pass &myObj).
So inside the callback you will dereference it in this way:
MyObject *myObject = (MyObject *)inClientData;
Now in your code you set the MusicSequenceCallback in this way:
MusicSequenceSetUserCallback(sequence, noteUserCallback, NULL);
the 3rd parameter is void *inClientData and it is this data that is passed as first parameter of the callback. So instead of NULL you must pass your object containing all the UIImageViews you need to use:
MusicSequenceSetUserCallback(sequence, noteUserCallback, myObject);
and then you will be able to use any obj-c method on this obj instance. The pointer to this object will be passed to the callback and then you will just need to dereference it inside the callback.

Related

unable to set values taken from text fields from a UIViewController to another class for calculation

I am having problems unable to set values taken from UItextfields from a Viewcontroller to another class which does the calculations from those values.
#interface InitialViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *phValue;
#property (weak, nonatomic) IBOutlet UITextField *pco2Value;
#property (weak, nonatomic) IBOutlet UITextField *hco3Value;
#property (weak, nonatomic) IBOutlet UITextField *sodiumValue;
#property (weak, nonatomic) IBOutlet UITextField *chlorideValue;
#property (nonatomic,retain)NSDecimalNumber* ph;
#property (nonatomic,retain) NSDecimalNumber* pco2;
#property (nonatomic,retain)NSDecimalNumber* hco3;
#property (nonatomic,retain)NSDecimalNumber* sodium;
#property (nonatomic,retain)NSDecimalNumber* chloride;
- (IBAction)analyseValues:(UIButton *)sender;
#end
in the implementation
-(void)values{
[self setPh:[[NSDecimalNumber alloc] initWithFloat:phValue.text.floatValue ]];
[self setPco2:[[NSDecimalNumber alloc] initWithFloat:pco2Value.text.floatValue]];
[self setHco3:[[NSDecimalNumber alloc ] initWithFloat:hco3Value.text.floatValue]];
[self setSodium:[[NSDecimalNumber alloc] initWithFloat:sodiumValue.text.floatValue] ];
[self setChloride:[[NSDecimalNumber alloc] initWithFloat:chlorideValue.text.floatValue] ];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self values];}
Then I have another class which takes these values ,but on running the program it runs but does assign values taken from the initialViewController.
#import <Foundation/Foundation.h>
#import "InitialViewController.h"
#interface AcidBaseCalculations : NSObject
#property (nonatomic) float ph;
#property (nonatomic) float pco2;
#property (nonatomic) float chloride;
#property (nonatomic) float sodium;
#property (nonatomic) float hco3;
#implementation AcidBaseCalculations
#synthesize ph,pco2,chloride,hco3,sodium;
-(void)calculations{
InitialViewController *bvc = [[InitialViewController alloc] init];
ph = bvc.ph.floatValue ;
pco2 = bvc.pco2.floatValue;
// these values are not being set although the program runs successfully
hco3 = bvc.hco3.floatValue;
sodium = bvc.sodium.floatValue;
chloride = bvc.chloride.floatValue;
I want to use these new values here and perform some logical operations in this class.
You are creating a new InitialViewController with this line:
InitialViewController *bvc = [[InitialViewController alloc] init];
It is a diffrent instance than your other/original InitialViewController
You either have to make a property
#property(nonatomic,strong)InitialViewController myInitialVC;
And set this property when you are alloc/initing your AcidBaseCalculations.
Our you have to make property for all variables you would like to calculate and set them while alloc/initing to the
AcidBaseCalculations

Add object to an array does not work, when I iterate through the array I see no objects

What I want:
On the iPhone user enters few values and tap on save button.
App should store the values in an instance of VehicleClass and add it to an NSMutableArray.
Hope you can help.
ViewController.h
#import <UIKit/UIKit.h>
#import "VehicleClass.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *producerTextField;
#property (weak, nonatomic) IBOutlet UITextField *modelTextField;
#property (weak, nonatomic) IBOutlet UITextField *pYearTextField;
#property (weak, nonatomic) IBOutlet UITextField *priceTextField;
#property (weak, nonatomic) NSMutableArray *carArray;
#property (weak, nonatomic) IBOutlet UILabel *statusLabel;
- (IBAction)addCar:(id)sender;
- (IBAction)showCars:(id)sender;
#end
ViewController.m
#synthesize producerTextField, modelTextField, pYearTextField, priceTextField, carArray;
- (void)viewDidLoad
{
[super viewDidLoad];
carArray = [NSMutableArray array];
}
- (IBAction)addCar:(id)sender
{
VehicleClass *newVehic = [[VehicleClass alloc]initWithProducer:producerTextField.text
andModel:modelTextField.text
andPYear:producerTextField.text
andPrice:priceTextField.text];
[carArray addObject:newVehic];
if (carArray != nil) {
statusLabel.text = #"Car saved to array.";
}
else {
statusLabel.text = #"Error, check your code.";
}
}
VehicleClass.h
#import <Foundation/Foundation.h>
#interface VehicleClass : NSObject
#property (weak, nonatomic) NSString *producer;
#property (weak, nonatomic) NSString *model;
#property (weak, nonatomic) NSString *pYear;
#property (weak, nonatomic) NSString *price;
-(id)initWithProducer:(NSString *)varProducer
andModel:(NSString *)varModel
andPYear:(NSString *)varPYear
andPrice:(NSString *)varPrice;
#end
VehicleClass.m
#import "VehicleClass.h"
#implementation VehicleClass
#synthesize model, price,pYear,producer;
-(id)initWithProducer:(NSString *)varProducer
andModel:(NSString *)varModel
andPYear:(NSString *)varPYear
andPrice:(NSString *)varPrice
{
self = [super init];
if (self != nil)
{
producer = varProducer;
model = varModel;
pYear = varPYear;
price = varPYear;
}
return self;
}
#end
Maybe the problem has something to do with my init method?
carArray is a weak property, it should be strong.
Also: you should use self.carArray and not carArray. When you use carArray, you are using the underlying iVar and not the actual property. It will cause confusion later.
Your properties should be, instead of property (weak, nonatomic), property (nonatomic, retain).
In your init method, put:
-(id)initWithProducer:(NSString *)varProducer
andModel:(NSString *)varModel
andPYear:(NSString *)varPYear
andPrice:(NSString *)varPrice
{
self = [super init];
if (self != nil)
{
self.producer = varProducer;
self.model = varModel;
self.pYear = varPYear;
self.price = varPYear;
}
return self;
}
Then, to check if your carArray has objects, instead of using if (carArray != nil), use if ([carArray count] > 0), because you already init it.
Your carArray is weak property. carArray should be strong.

UIViewController difficulty

//RootViewViewController.h
#import <UIKit/UIKit.h>
#import "SettingsViewController.h"
#import "OneSlotViewController.h"
#import "TwoSlotViewController.h"
#import "BingoSlotViewController.h"
#import "SettingsViewController.h"
#interface RootViewViewController : UIViewController{
IBOutlet UIButton *owaru;
OneSlotViewController *oneSlotViewController;
TwoSlotViewController *button2ViewController;
BingoSlotViewController *button3ViewController;
UIViewController *pushedController;
UIButton *hajimekara;
SettingsViewController *settingsVc;
}
#property (retain, nonatomic) UIButton *hajimekara;
#property (strong, nonatomic) IBOutlet UIButton *owaru;
#property (nonatomic, retain) OneSlotViewController *button1ViewController;
#property (nonatomic, retain) TwoSlotViewController *button2ViewController;
#property (nonatomic, retain) BingoSlotViewController *button3ViewController;
#property (nonatomic, retain) UIViewController *pushedController;
#property (nonatomic, retain) SettingsViewController *settingsVc;
//RootViewViewController.m
#synthesize button1ViewController;
#synthesize button2ViewController;
#synthesize button3ViewController;
#synthesize pushedController;
#synthesize settingsVc;
-(IBAction) startButtonPressed:(id) sender {
if (self.settingsVc.pushedController!=nil) {
NSLog(#"push");
[self presentViewController:self.settingsVc.pushedController animated:YES completion:NULL];
}
}
//SettingViewController.h
#import "OneSlotViewController.h"
#import "TwoSlotViewController.h"
#import "BingoSlotViewController.h"
#import "SettingsViewController.h"
#import "AGImagePickerController.h"
#class RootViewViewController;
#interface SettingsViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIPopoverControllerDelegate,UIScrollViewDelegate>{
OneSlotViewController *oneSlotViewController;
TwoSlotViewController *button2ViewController;
BingoSlotViewController *button3ViewController;
UIViewController *pushedController;
RootViewViewController *mainVc;
UIImageView *lastPriceView;
CustomImagePicker *_imagePicker;
UINavigationController *navController;
}
#property (nonatomic, retain) UIPopoverController *popoverController;
#property (nonatomic, retain) OneSlotViewController *button1ViewController;
#property (nonatomic, retain) TwoSlotViewController *button2ViewController;
#property (nonatomic, retain) BingoSlotViewController *button3ViewController;
#property (nonatomic, retain) UIViewController *pushedController;
#property (nonatomic, retain) RootViewViewController *mainVc;
#property (strong, nonatomic) IBOutlet UIImageView *lastPriceView;
#property (nonatomic, retain) CustomImagePicker *imagePicker;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
//SettingViewController.m
-(IBAction) button1Pressed:(id)sender {
if (self.button1ViewController==nil) {
button1ViewController = [[OneSlotViewController alloc] init];
}
self.pushedController = button1ViewController;
}
-(IBAction) button2Pressed:(id)sender {
if (self.button2ViewController==nil) {
button2ViewController = [[TwoSlotViewController alloc] init];
}
self.pushedController = button2ViewController;
}
-(IBAction) button3Pressed:(id)sender {
if (self.button3ViewController==nil) {
button3ViewController = [[BingoSlotViewController alloc] init];
}
self.pushedController = button3ViewController;
}
I have already declare there instances un the two controllers.
Whenever a button is pressed in the SettingsViewController it will pass the ViewController to the MainViewController's startButton.
But I cant seem to make it work. Thanks for your help.
SettingsViewController.pushedController and MainViewController.pushedController are separate variables. Changing one will not effect the other.
You have two options. You either need to store the SettingsViewController in the MainViewController, or pass the MainViewController to SettingsViewController.
If MainViewController keeps a reference to the SettingsViewController, then you can:
-(IBAction) startButtonPressed:(id) sender {
if (self.settingsViewController.pushedController!=nil) {
NSLog(#"push");
[self presentViewController:self.settingsViewController.pushedController animated:YES completion:NULL];
}
}
If SettingsViewController is passed a reference to MainViewController, then you can:
-(IBAction) button1Pressed:(id)sender {
if (self.button1ViewController==nil) {
button1ViewController = [[ViewOneController alloc] init];
}
self.mainViewController.pushedController = button1ViewController;
}
-(IBAction) button2Pressed:(id)sender {
if (self.button2ViewController==nil) {
button2ViewController = [[ViewTwoController alloc] init];
}
self.mainViewController.pushedController = button2ViewController;
}
-(IBAction) button3Pressed:(id)sender {
if (self.button3ViewController==nil) {
button3ViewController = [[ViewThreeController alloc] init];
}
self.mainViewController.pushedController = button3ViewController;
}
Pick one of these things and you should be OK.

Get values from several UITextFields

I have a grouped static UITableViewController, with 3 rows per section. Each row contains a UITextField. How do I get the value for each UITextField?
So far, what I've seen is how to get the value of one UITextField. What should I do if I have to get more than one.
I plan to put the values of these fields in a NSMutableDictionary.
Here's my code for making the table view: (irrelevant parts are omitted)
#import "AddItemTableViewController.h"
#import "iMonggoFetcher.h"
#interface AddItemTableViewController() <UITextFieldDelegate>
#property (weak, nonatomic) NSMutableDictionary *productItem;
//required
#property (weak, nonatomic) IBOutlet UITextField *stockNoTextField;
#property (weak, nonatomic) IBOutlet UITextField *nameTextField;
#property (weak, nonatomic) IBOutlet UITextField *retailPriceTextField;
//basic info
#property (weak, nonatomic) IBOutlet UITextField *costTextField;
#property (weak, nonatomic) IBOutlet UITextField *descriptionTextField;
#property (weak, nonatomic) IBOutlet UITextField *barcodesTextField;
#property (weak, nonatomic) IBOutlet UITextField *tagsTextField;
#property (weak, nonatomic) IBOutlet UISwitch *exemptFromTaxSwitch;
//advanced features
#property (weak, nonatomic) IBOutlet UISwitch *allowDecimalQuantitiesSwitch;
#property (weak, nonatomic) IBOutlet UISwitch *enableOpenPriceSwitch;
#property (weak, nonatomic) IBOutlet UISwitch *disableDiscountSwitch;
#property (weak, nonatomic) IBOutlet UISwitch *disableInventorySwitch;
#end
#implementation AddItemTableViewController
#synthesize productItem = _productItem;
#synthesize stockNoTextField = _stockNoTextField;
#synthesize nameTextField = _nameTextField;
#synthesize retailPriceTextField = _retailPriceTextField;
#synthesize costTextField = _costTextField;
#synthesize descriptionTextField = _descriptionTextField;
#synthesize barcodesTextField = _barcodesTextField;
#synthesize tagsTextField = _tagsTextField;
#synthesize exemptFromTaxSwitch = _exemptFromTaxSwitch;
#synthesize allowDecimalQuantitiesSwitch = _allowDecimalQuantitiesSwitch;
#synthesize enableOpenPriceSwitch = _enableOpenPriceSwitch;
#synthesize disableDiscountSwitch = _disableDiscountSwitch;
#synthesize disableInventorySwitch = _disableInventorySwitch;
- (IBAction)save:(UIBarButtonItem *)sender {
[self.productItem setValue:self.stockNoTextField.text forKey:IMONGGO_PRODUCT_STOCK_NO];
[self.productItem setValue:self.nameTextField.text forKey:IMONGGO_PRODUCT_NAME];
[self.productItem setValue:self.retailPriceTextField.text forKey:IMONGGO_PRODUCT_RETAIL_PRICE];
}
#pragma mark - UITextField
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
// optionally trigger delegate method here
}
#pragma mark - View lifecycle
- (void) viewWillAppear:(BOOL)animated{
//[self.stockNoTextField becomeFirstResponder];
}
- (void) viewDidLoad{
[super viewDidLoad];
self.stockNoTextField.delegate = self;
self.nameTextField.delegate = self;
self.retailPriceTextField.delegate = self;
//will do the rest later, just trying it out for now
}
#end
1) You can make properties of these fields.. and then get the text by the property name.text
2) You can try giving the fields a tag and then get these fields using viewwithtag method. and then their text.
you can take values of each textFields in a array by textField.text.

Load a view .xib in MainWindow.xib

It`s probably a very silly mistake, but I've spent over 4 days looking for a solution for this.
It is very simple, I´ve got my MainView.xib and a view called FirstViewController (h/m/xib).
In MainWindow.xib I add a UIViewController and change the class name to FirstViewController and set the Nib name also (altouhg I've tried both ways).
I guess it has to do something with outlets, but I can`t really tell, as I am a newbie developing for iOS, any help wil REALLY help a lot.
Im using XCode 3.2 and interface builder, with SDK 4.3
AppDelegate
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface iPadTerritorioV2AppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet UIViewController *navigationController;
NSString *devToken;
NSString *matricula;
NSString *campus;
NSMutableArray *materiasAlumno; //para CCM
NSMutableArray *busqDir; //para CCM
NSInteger agendaBadgeNumber;
NSInteger intramurosBadgeNumber;
NSInteger notificacionesBadgeNumber;
NSInteger mapaBadgeNumber;
NSMutableData *receivedData;
NSMutableDictionary *listData;
BOOL yaSeHizoElPrimerFetchBadges;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIViewController *navigationController;
#property (nonatomic, retain) NSString *devToken;
#property (nonatomic, retain) NSString *matricula;
#property (nonatomic, retain) NSString *campus;
#property (nonatomic, retain) NSMutableArray *materiasAlumno;
#property (nonatomic, retain) NSMutableArray *busqDir;
#property NSInteger agendaBadgeNumber;
#property NSInteger intramurosBadgeNumber;
#property NSInteger notificacionesBadgeNumber;
#property NSInteger mapaBadgeNumber;
#property (nonatomic, retain) NSMutableData *receivedData;
#property (nonatomic, retain) NSMutableDictionary *listData;
#property BOOL yaSeHizoElPrimerFetchBadges;
- (void)fetchBadges;
#end
FirstViewController.h
#import <UIKit/UIKit.h>
#import "Constants.h"
#import "StringDecoding.h"
#define kConnectionBadgeNotifications 0
#define kConnectionLogin 1
#define kConnectionDevToken 2
#define kCCMindex 0
#define kCSFindex 1
#define kMTYindex 2
#interface FirstViewController : UIViewController {
IBOutlet UISegmentedControl *segmentedCampus;
IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;
IBOutlet UISwitch *remembermeSwitch;
IBOutlet UIButton *loginButton;
UIActivityIndicatorView *loginIndicator;
NSMutableDictionary *listData;
NSMutableData *receivedData;
NSInteger connectionID;
}
#property (nonatomic, retain) UISegmentedControl *segmentedCampus;
#property (nonatomic, retain) UITextField *usernameField;
#property (nonatomic, retain) UITextField *passwordField;
#property (nonatomic, retain) UIActivityIndicatorView *loginIndicator;
#property (nonatomic, retain) UISwitch *remembermeSwitch;
#property (nonatomic, retain) UIButton *loginButton;
#property (nonatomic, retain) NSMutableDictionary *listData;
#property (nonatomic, retain) NSMutableData *receivedData;
#property NSInteger connectionID;
- (IBAction)handleNextClick:(id) sender;
- (IBAction)backgroundClick;
- (IBAction)login: (id) sender;
#end
It sounds like your FirstViewController isn't being retained - if it's not assigned to an outlet anywhere, nothing's retaining it and it'll just disappear. Add a property somewhere (your AppDelegate, perhaps, if that's all you've got) and connect it to the controller:
#property (nonatomic, retain) IBOutlet UIViewController *firstViewController;