Get values from several UITextFields - iphone

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.

Related

Display UIPickerView when UITextField is clicked

I have a screen with many text fields on it. For one particular text field, I need it to show a picker from which the user can pick state names, once the user has picked a state by clicking on the states name, the picker should disappear.
I am not quite sure how to do this, I have made the Nib file which looks like this.
And here is my header file:
#import <UIKit/UIKit.h>
#import "APIHelper.h"
#interface AddNewAddressViewController : UIViewController <UIPickerViewDelegate,
UIPickerViewDataSource, UITextFieldDelegate>
{
APIHelper *myAPIHelper;
NSMutableData *responseData;
NSURLConnection *theConnection;
UIPickerView *picker;
NSArray *stateNames;
#public IBOutlet UIImageView *imageNewAddressBox;
#public IBOutlet UIImageView *imageNewAddressBox1;
#public IBOutlet UIImageView *imageNewAddressBox2;
#public IBOutlet UIImageView *imageNewAddressBox3;
#public IBOutlet UILabel *labelNewAddress;
#public IBOutlet UILabel *labelStreetAddress;
#public IBOutlet UILabel *labelStreetAddressTwo;
#public IBOutlet UILabel *labelZipCode;
#public IBOutlet UILabel *labelState;
#public IBOutlet UILabel *labelCity;
#public IBOutlet UILabel *labelPhoneNumber;
#public IBOutlet UITextField *textFieldFullName;
#public IBOutlet UITextField *textFieldStreetAddress;
#public IBOutlet UITextField *textFieldPhoneNumber;
#public IBOutlet UITextField *textFieldStreetAddressTwo;
#public IBOutlet UITextField *textFieldCity;
#public IBOutlet UITextField *textFieldState;
#public IBOutlet UITextField *textFieldZipCode;
#public IBOutlet UIButton *buttonNext;
}
#property (nonatomic, strong) IBOutlet UIPickerView *picker;
#property (nonatomic, strong) NSArray *stateNames;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox1;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox2;
#property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox3;
#property (nonatomic, strong) IBOutlet UILabel *labelNewAddress;
#property (nonatomic, strong) IBOutlet UILabel *labelStreetAddress;
#property (nonatomic, strong) IBOutlet UILabel *labelStreetAddressTwo;
#property (nonatomic, strong) IBOutlet UILabel *labelZipCode;
#property (nonatomic, strong) IBOutlet UILabel *labelState;
#property (nonatomic, strong) IBOutlet UILabel *labelCity;
#property (nonatomic, strong) IBOutlet UILabel *labelPhoneNumber;
#property (nonatomic, strong) IBOutlet UITextField *textFieldFullName;
#property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddress;
#property (nonatomic, strong) IBOutlet UITextField *textFieldPhoneNumber;
#property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddressTwo;
#property (nonatomic, strong) IBOutlet UITextField *textFieldCity;
#property (nonatomic, strong) IBOutlet UITextField *textFieldState;
#property (nonatomic, strong) IBOutlet UITextField *textFieldZipCode;
#property (nonatomic, strong) IBOutlet UIButton *buttonNext;
-(IBAction)addressTextFieldShouldReturn:(id)sender;
-(IBAction)nextPressed;
#end
And here is my .M file:
#import "AddNewAddressViewController.h"
#implementation AddNewAddressViewController
#synthesize imageNewAddressBox;
#synthesize imageNewAddressBox1;
#synthesize imageNewAddressBox2;
#synthesize imageNewAddressBox3;
#synthesize labelNewAddress;
#synthesize labelStreetAddress;
#synthesize labelStreetAddressTwo;
#synthesize labelZipCode;
#synthesize labelState;
#synthesize labelCity;
#synthesize labelPhoneNumber;
#synthesize textFieldFullName;
#synthesize textFieldStreetAddress;
#synthesize textFieldPhoneNumber;
#synthesize textFieldStreetAddressTwo;
#synthesize textFieldCity;
#synthesize textFieldState;
#synthesize textFieldZipCode;
#synthesize buttonNext;
#synthesize stateNames;
#synthesize picker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.stateNames = [[NSArray alloc] initWithObjects:#"Alabama", #"Alaska", #"Arizona", #"Arkansas", #"California", #"Colorado", #"Connecticut", #"Delaware", #"District of Columbia", #"Florida", #"Georgia", #"Guam", #"Hawaii", #"Idaho", #"Illinois", #"Indiana", #"Iowa", #"Kansas", #"Kentucky", #"Louisiana", #"Maine", #"Maryland", #"Massachusetts", #"Michigan", #"Minnesota", #"Mississippi", #"Missouri", #"Montana", #"Nebraska", #"Nevada", #"New Hampshire", #"New Jersey", #"New Mexico", #"New York", #"North Carolina", #"North Dakota", #"Ohio", #"Oklahoma", #"Oregon", #"Pennsylvania", #"Puerto Rico", #"Rhode Island", #"South Carolina", #"South Dakota", #"Tennessee", #"Texas", #"Utah", #"Vermont", #"Virginia", #"Virgin Islands", #"Washington", #"West Virginia", #"Wisconsin", #"Wyoming", nil];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// clicking outside the textfields removes keyboard
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(addressTextFieldShouldReturn:)];
[self.view addGestureRecognizer:tap];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setTitle: #"Update Address Book"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
-(IBAction)addressTextFieldShouldReturn:(id)sender
{
[textFieldFullName resignFirstResponder];
[textFieldStreetAddress resignFirstResponder];
[textFieldStreetAddressTwo resignFirstResponder];
[textFieldPhoneNumber resignFirstResponder];
[textFieldState resignFirstResponder];
[textFieldCity resignFirstResponder];
[textFieldZipCode resignFirstResponder];
[sender resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [stateNames count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component
{
return [stateNames objectAtIndex:row];
}
#end
So, all I want it do is, when the user clicks on the textFieldState, a picker with all of the states name in it pops up from the bottom and then the user clicks on the states name and that name appears in textFieldState.
Any help would be greatly appreciated. Thanks.
you can use TextFielđelegate , is worked
#pragma mark- text delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField==txtMonth) {
[txtMonth resignFirstResponder];
if (monthClick==0) {
monthPicker.hidden=NO;
monthPicker = [[AFPickerView alloc] initWithFrame:CGRectMake(98.0, 123.0, 126.0, 197.0)];
monthPicker.dataSource = self;
monthPicker.delegate = self;
[monthPicker reloadData];
if ([txtMonth.text isEqualToString:#""]) {
txtMonth.text=[NSString stringWithFormat:#"%i",1];
}
[self.view addSubview:monthPicker];
monthClick=1;
}
}
}
I think you need to implement delegate methods for UITextField, when the
- (void)textFieldDidBeginEditing:(UITextField *)textField
method is called you set the frame of uipicker and show it.

Focus on UITextField does not work

In my iPhone application I have a UIScrollView with several UITextFields.
Using BSKeyboardControls I have added Prev/Next/Done buttons to move between the fields. However, the focus on the selected field is not working, meaning that the text field is actually still under the keyboard although selected.
becomeFirstResponder is activated but just don't set the focus.
Any ideas what might be wrong?
Thanks
In the H file
#import "BSKeyboardControls.h"
...
#interface AddClientViewController : BaseViewController<UIAlertViewDelegate, UIScrollViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UITextViewDelegate, BSKeyboardControlsDelegate>
...
#property (strong, nonatomic) IBOutlet UITextField *firstName;
#property (strong, nonatomic) IBOutlet UITextField *lastName;
#property (strong, nonatomic) IBOutlet UITextField *email;
#property (strong, nonatomic) IBOutlet UITextField *mobile;
#property (strong, nonatomic) IBOutlet UITextField *birthday;
#property (strong, nonatomic) IBOutlet UITextField *anniversary;
#property (strong, nonatomic) IBOutlet UITextField *street;
#property (strong, nonatomic) IBOutlet UITextField *city;
#property (strong, nonatomic) IBOutlet UITextField *state;
#property (strong, nonatomic) IBOutlet UITextField *zip;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) BSKeyboardControls *keyboardControls;
....
In M file
- (void)viewDidLoad
{
...
NSArray *fields = #[ self.firstName, self.lastName,
self.email, self.mobile,
self.birthday, self.anniversary,
self.street, self.city, self.state, self.zip];
[self setKeyboardControls:[[BSKeyboardControls alloc] initWithFields:fields]];
[self.keyboardControls setDelegate:self];
}
- (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls
{
[keyboardControls.activeField resignFirstResponder];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls directionPressed:(BSKeyboardControlsDirection)direction
{
UIView *view = keyboardControls.activeField.superview.superview;
[self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls selectedField:(UIView *)field inDirection:(BSKeyboardControlsDirection)direction
{
UIView *view = keyboardControls.activeField.superview.superview;
[self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.keyboardControls setActiveField:textField];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self.keyboardControls setActiveField:textView];
}
The setActiveField in BSKeyboardControls
- (void)setActiveField:(id)activeField
{
if (activeField != _activeField)
{
if ([self.fields containsObject:activeField])
{
_activeField = activeField;
if (![activeField isFirstResponder])
{
[activeField becomeFirstResponder];
}
[self updateSegmentedControlEnabledStates];
}
}
}
Since you're using a UIScrollView with UITextFields, you can use the scrollRectToVisible method for the UIScrollview, in a method that'd be roughly like this:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[_myScrollView scrollRectToVisible:textField.frame animated:YES];
}
To do this, you'll need to make sure that the UIViewController is the UITextFieldDelegate of each of the textfields in the scrollview. You could do this in the viewDidLoad method of your UIViewController:
[textField1 setDelegate:self];
[textField2 setDelegate:self];
...and so on

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.

Make an IBOutlet UIImageView accessible from inside of a methods

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.

type of property txtname (uiTextView *) does not match of ivar txtname(uiTextView)

// ViewController.h
#interface ViewController : UIViewController{
IBOutlet UIButton *btnname;
IBOutlet UITextView *txtname;
IBOutlet UILabel *lblname;
}
#property (retain, nonatomic) IBOutlet UITextField *txtname;
#property (retain, nonatomic) IBOutlet UILabel *lblname;
- (IBAction)Btntikla:(id)sender;
#end
//ViewController.m
#implementation ViewController
#synthesize txtname;//here it says -type of property txtname (uiTextView *) does not match of ivar txtname(uiTextView)- this mistakes
#synthesize lblname;
- (void)dealloc {
[lblname release];
[txtname release];
[super dealloc];
}
- (IBAction)Btntikla:(id)sender {
NSString *name=[txtname text];
lblname.text=name;
}
#end
//my whole program is this.it gives this error?type of property txtname (uiTextView *) does not match of ivar txtname(uiTextView)..Can you help me pls?? what is my mistake ?
That's beacause you have
#property (retain, nonatomic) IBOutlet UITextField *txtname;
and
IBOutlet UITextView *txtname;
And UITextField and UITextView are differents controls