Multiple toolbar items in a single row - iphone

i just want to create multiple toolbar items in a single row here what i did...
NSMutableArray *barButtonArray = [[NSMutableArray alloc] init];
for (int i=0; i<[[State getSubCategoryids] count]; i++) {
NSString *nameString = [NSString stringWithFormat:#"%#",[[State getSubCategoryNames] objectAtIndex:i]];
NSLog(#"nameString: %#", nameString);
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithTitle:nameString style:UIBarButtonItemStyleBordered target:nil action:#selector(productImages)];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[barButtonArray addObject:customBarButton];
[barButtonArray addObject:flexItem];
[flexItem release];
[customBarButton release];
}
for (int i = 0; i<[barButtonArray count]; i++) {
NSLog(#"barbutton items for loop");
NSArray *items = [NSArray arrayWithObjects:[barButtonArray objectAtIndex:i],nil];
NSLog(#"items: %#", items);
[toolbar setItems:items animated:NO];
}
but it is not showing anything in toolbar ...... any suggestions...?

What is the second for-loop for?
You already have an array of items (barButtonArray).
Replace the second for-loop with this:
[toolbar setItems:barButtonArray animated:NO];

In your last for loop you are redeclaring the items array and calling [toolbar setItems:] over and over. Just do this:
[toolbar setItems:barButtonItems animated:NO];

Related

pickerview selectedRowInComponent always returning 0

I am using selectedRowInComponent method to get the presently selected row on component with index 0 but it is always returning 0. I have checked it in
NSLog("%d",[pickerview selectedRowInComponent:0]);
I have initialized the picker as I am using picker view programmatically. Please Suggest.
I hope this code will help you.I am using it in my project its working perfect.
Call this function it ViewDidLoad
[self loadFirstPicker];
Below is function to make UIpicker which shows only time. (And Save in text feild)
-(void)loadFirstPicker
{
UIDatePicker *picker = [[UIDatePicker alloc] init];
picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
picker.datePickerMode = UIDatePickerModeTime;
[picker addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
picker.frame = CGRectMake(0.0, 250, pickerSize.width, 460);
picker.backgroundColor = [UIColor blackColor];
checkEveryTextFeild.inputView = picker;
//[self.view addSubview:picker];
//[picker release];
// Create done button in UIPickerView
UIToolbar* mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 220, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
checkEveryTextFeild.inputAccessoryView = mypickerToolbar;
}
This Function getting the time from picker and setting it to textFeild
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HH:mm:ss"];
//[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
checkEveryTextFeild.text = [dateFormatter stringFromDate:[sender date]];
NSDate *currentDate = [NSDate date];
NSLog(#"%#",currentDate);
NSLog(#"Picked the date %#", [dateFormatter stringFromDate:[sender date]]);
}
you might want to try setting both of them. the second line will retrieve the pickerview that the class detects:
[filterPicker selectRow:selectedPickerIndex inComponent:0 animated:NO];
[self pickerView:filterPicker didSelectRow:selectedPickerIndex inComponent:0];
self.personInfoPicker.delegate = self;
self.personInfoPicker.dataSource = self;
[self.personInfoPicker reloadAllComponents];

NSMutableArray not adding object correctly?

I have a button that saves cell's data upon ButtonClick
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
[self.checkbox setFrame:checkboxRect];
[self.checkbox setImage:[UIImage imageNamed:#"unselected#2x.png"]forState:UIControlStateNormal];
[self.checkbox setImage:[UIImage imageNamed:#"selected#2x.png"] forState:UIControlStateSelected];
[self.checkbox addTarget:self action:#selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
self.accessoryView = self.checkbox;
array = [NSMutableArray array];
}
return self;
}
-(void)checkboxClicked:(UIButton *)sender{
sender.selected = !sender.selected;
UITableViewCell *cell = (AddressBookCell *)sender.superview;
if(sender.selected){
[array addObject:cell];
}
}
and i use that array in my other class, but it keeps giving me empty arrays;
this is my other class, it gives me a log of Zero.
-(void)textMessage{
/*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Not Implmeneted Yet" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
[alert release];*/
AddressBookCell *names = [[AddressBookCell alloc]init];
NSLog(#"%d",[[names array]count]);
}
![1] http://min.us/m2GMsoRap
I need a way to store data upon button click and transfer it to my other classes.
EDIT## full code
#import "AddressBookCell.h"
#implementation AddressBookCell
#synthesize checkbox;
#synthesize array;
#synthesize addressController;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
[self.checkbox setFrame:checkboxRect];
[self.checkbox setImage:[UIImage imageNamed:#"unselected#2x.png"]forState:UIControlStateNormal];
[self.checkbox setImage:[UIImage imageNamed:#"selected#2x.png"] forState:UIControlStateSelected];
[self.checkbox addTarget:self action:#selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
self.accessoryView = self.checkbox;
array = [[NSMutableArray alloc]init];
}
return self;
}
-(void)checkboxClicked:(UIButton *)sender{
sender.selected = !sender.selected;
UITableViewCell *cell = (AddressBookCell *)sender.superview;
NSLog(#"%d'",cell.tag);
if(sender.selected){
[array addObject:cell];
}else{
if([array containsObject:cell]){
[array removeObject:cell];
}
NSLog(#"%d", [array count]);
}
}
and now my other class
-(void)setUpContacts{
NSDictionary *alphabet = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:0],#"A",[NSNumber numberWithInt:1],#"B",[NSNumber numberWithInt:2],#"C",[NSNumber numberWithInt:3],#"D",[NSNumber numberWithInt:4],#"E",[NSNumber numberWithInt:5],#"F",[NSNumber numberWithInt:6],#"G",[NSNumber numberWithInt:7],#"H",[NSNumber numberWithInt:8],#"I",[NSNumber numberWithInt:9],#"J",[NSNumber numberWithInt:10],#"K",[NSNumber numberWithInt:11],#"L",[NSNumber numberWithInt:12],#"M",[NSNumber numberWithInt:13],#"N",[NSNumber numberWithInt:14],#"O",[NSNumber numberWithInt:15],#"P",[NSNumber numberWithInt:16],#"Q",[NSNumber numberWithInt:17],#"R",[NSNumber numberWithInt:18],#"S",[NSNumber numberWithInt:19],#"T",[NSNumber numberWithInt:20],#"U",[NSNumber numberWithInt:21],#"V",[NSNumber numberWithInt:22],#"W",[NSNumber numberWithInt:23],#"X",[NSNumber numberWithInt:24],#"Y",[NSNumber numberWithInt:25],#"Z", nil];
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
for(int i = 0; i<=27; i++){
[tempArray addObject:[NSNull null]];
}
Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];
for (NSDictionary* info in contactNumbers) {
firstLetter = [info objectForKey:#"lastName"];
int index = 27;
if([firstLetter length] > 0){
firstLetter =[NSString stringWithFormat:#"%C",[firstLetter characterAtIndex:0]];
firstLetter= [firstLetter capitalizedString];
if([alphabet objectForKey:firstLetter]){
NSNumber *t = [alphabet valueForKey:firstLetter];
index = [t intValue] + 1;
}
}
if([tempArray objectAtIndex:index] == [NSNull null]){
[tempArray insertObject:[NSMutableArray array] atIndex:index];
}
[[tempArray objectAtIndex:index] addObject:info];
}
[alphabet release];
NSArray *alphabet2 = [[NSArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
NSMutableArray *tempArray2 = [[NSMutableArray alloc]init];
NSMutableArray *titleTemp = [[NSMutableArray alloc]init];
int c = 0;
for(int i = 0; i<=27; i++){
if([tempArray objectAtIndex:i] != [NSNull null]){
if(i == 0){
[titleTemp insertObject:#"Suggested" atIndex:c];
}else if(i == 27){
[titleTemp insertObject:#"Others" atIndex:c];
}else{
int loc = i -1;
[titleTemp insertObject:[alphabet2 objectAtIndex:loc] atIndex:c];
}
[tempArray2 insertObject:[tempArray objectAtIndex:i] atIndex:c];
c++;
}
}
[alphabet2 release];
[tempArray release];
letterArray = tempArray2;
titlePointer = titleTemp;
}
-(void)textMessage{
/*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Not Implmeneted Yet" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
[alert release];*/
AddressBookCell *names = [[AddressBookCell alloc]init];
savedArray = [NSArray arrayWithArray:[names array]];
NSLog(#"%#",savedArray);
}
- (void)viewDidLoad{
[super viewDidLoad];
[self setUpContacts];
indexPaths = [[NSMutableDictionary alloc]init];
//suggestedPeople = [[NSArray alloc]initWithObjects:#"User1",#"User2",#"User3",#"User4", nil];
//set up the tableView
self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.title = #"Select Friends";
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Text"style:UIBarButtonItemStylePlain target:self action:#selector(textMessage)];
//testing section
}
Of course it's empty, you alloc/init'd a new instance of AdressBookCell in your other class, so it doesn't have anything in its array. You need a property in your other class that points to the instance of your first class where you fill your array, and then you can use [pointerToFirstClass array] to get at that array.
It looks like you are using manual memory management. If not, you need to specify.
In the case that you are, when you create the array with the line [NSMutableArray array], you are creating an autoreleased object that will be released before your other class tries to access it.
You'll need to retain it at the point you create it, and release it in your dealloc. If you change [NSMutableArray array] to [[NSMutableArray array] retain] and add an [array release] line to your dealloc the array won't disappear on you until you release the owning object.
you haven't included enough info to tell what is going on, but I can guess...
a log of zero could mean an empty array, or more likely a nil array... try this:
AddressBookCell *names = [[AddressBookCell alloc]init];
NSLog(#"%#",[names array]);

toggle between UIBarButtonSystemItemPlay and UIBarButtonSystemItemPause

Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:#selector(playaudio:)];
systemItem1.style = UIBarButtonItemStyleBordered;
UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPause
target:self
action:#selector(pause:)];
systemItem2.style = UIBarButtonItemStyleBordered;
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, modalBarButtonItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[modalBarButtonItem release];
[flexItem release];
-(void) playaudio: (id) sender {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[audioPlayer play];
[fileURL release];
}
- (void)pause: (id)sender {
if
([audioPlayer isPlaying])
{[audioPlayer pause];}
else
{[audioPlayer play];}
}
Any ideas how i can do that.
Essentially, every time the play/pause status is updated, you're going to want to run a method to update the toolbar. Something like this should work.
You can create a method like this:
-(void)playPause{
if(audioPlayer == nil){
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme" ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[fileURL release];
}
UIBarButtonSystemItem buttontype = UIBarButtonSystemItemPlay;
if([audioPlayer isPlaying]){
[audioPlayer pause];
}
else {
[audioPlayer play];
buttontype = UIBarButtonSystemItemPause;
}
UIBarButtonSystemItem *item = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:buttontype
target:self
action:#selector(playPause)] autorelease];
self.toolbar.items = [NSArray arrayWithObject:item];
}
I had a much simpler solution to that issue:
- (void) setStartStopButton:(BOOL)startorstop
{
UIBarButtonItem *startStopButton = nil;
if (startorstop == YES) {
startStopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:#selector(startStopAction:)];
}
else
{
startStopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(startStopAction:)];
}
self.navigationItem.rightBarButtonItem = startStopButton;
[startStopButton release];
}
- (IBAction)startStopAction:(id)sender
{
if (task.isActive)
{
[task stopTask];
}
else
{
[task startTask];
}
[self setStartStopButton:task.isActive];
}
And then I call the first method to set the button in viewWillAppear as well to set the button before the view appears onscreen.

UIButton view unrecognized selector sent to instance

Basically i have a UIToolbar on the main window and when i hit the infobutton to open a modal view application is crashing with output message that UIButton view unrecognized selector sent to instance. Can someone please help me to solve this output message.
UIButton *infoItem = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoItem.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
infoItem.backgroundColor = [UIColor clearColor];
[infoItem addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
- (void) playaudio: (id) sender {
// Get the file path to the song to play.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme"
ofType:#"mp3"];
// Convert the file path to a URL.
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
//Initialize the AVAudioPlayer.
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
// Making sure the audio is at the start of the stream.
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
}
- (void)pause: (id)sender {
if
([audioPlayer isPlaying])
{[audioPlayer pause];}
else
{[audioPlayer play];}
}
- (void)stop: (id) sender
{
[audioPlayer stop];
}
- (void)displayModalViewaction
{
self.viewController = [[Infoviewcontroller alloc] init];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[self.view addSubview:navigationController.view];
}
Appreciate help to solve this.
Thanks in advance.
[infoItem addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
should be
[infoItem addTarget:self action:#selector(displayModalViewaction) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
because displayModalViewaction do not receive any parameters.
use #selector(displayModalViewaction) to add event;
or change method define with: -(void)displayModalViewaction:(id)sender;

Load mapview in the correct way

I am loading a mapview with annotation. I have written the code as shown below. As I am new to OOPS I know that I may have committed a lot of mistakes. It would be really helpful, if someone would review my code and give some suggestions.
- (void)viewDidLoad
{
[super viewDidLoad];
if(groupOrContactSelected){
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.infoButton];
UIBarButtonItem *segmentedButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *toolbarItems = [NSArray arrayWithObjects: infoButtonItem, flexibleSpace, segmentedButton, nil];
[self setToolbarItems:toolbarItems];
self.navigationController.toolbar.translucent = true;
self.navigationController.toolbarHidden = NO;
[infoButtonItem release];
[segmentedButton release];
[flexibleSpace release];
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
mapView.delegate=self;
[self.view addSubview:mapView];
addressbook = ABAddressBookCreate();
for (i = 0; i<[groupContentArray count]; i++) {
person = ABAddressBookGetPersonWithRecordID(addressbook,[[groupContentArray objectAtIndex:i] intValue]);
ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);
for (NSDictionary *addressDict in address)
{
addAnnotation = nil;
firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *country = [addressDict objectForKey:#"Country"];
NSString *streetName = [addressDict objectForKey:#"Street"];
NSString *cityName = [addressDict objectForKey:#"City"];
NSString *stateName = [addressDict objectForKey:#"State"];
NSString *fullAddress = [streetName stringByAppendingFormat:#"%#/%#/%#", cityName, stateName, country];
mapCenter = [self getLocationFromAddressString:fullAddress];
if(stateName != NULL || country != NULL || streetName != NULL || cityName != NULL){
addAnnotation = (SJAddressAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:[groupContentArray objectAtIndex:i]];
if(addAnnotation == nil){
addAnnotation = [[[SJAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:[groupContentArray objectAtIndex:i] ] autorelease];
[mapView addAnnotation:addAnnotation];}
}
}
CFRelease(addressProperty);
}
[self zoomToFitMapAnnotations:mapView];
[self.navigationItem setHidesBackButton:YES animated:YES];
NSString *mapTitle = localizedString(#"MAP_TITLE");
[self setTitle:mapTitle];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
NSString *close = localizedString(#"CLOSE");
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:close style:UIBarButtonItemStylePlain target:self action:#selector(onClose:)];
self.navigationItem.rightBarButtonItem = closeButton;
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[closeButton release];
}
[searchDirectionSegmentedControl release];
[mapView release];
}
I may have committed a lot of mistakes. All suggestions will be appreciated. Thanks
Here is link which you can prefer and know how to structure the code properly http://www.highoncoding.com/Articles/804_Introduction_to_MapKit_Framework_for_iPhone_Development.aspx
Hope this help you.