UITabBar Action in a loop - iphone

I have created a view loading to it toolbar with few 7 buttons.
I want to add action for each - thus each one will perform different action all of them will load view but according to the user selection of the button I will load to the view different screen i.e buttons etc..
Here is my loop:
int i =0;
for (NSString *items in array)
{
//Set button style
[barButton setStyle:UIBarButtonItemStyleBordered];
//Set the button title
[barButton setTitle:[array objectAtIndex:i]];
[barButton setAction:#selector(tmp:)];
//Add the bar button to the array - later will load the array to the tool bar items list.
[itemsArray insertObject:barButton atIndex:i];
i++;
}
As you see setAction is now activate tmp method - I want to take it from the name of the method from the NSString value of the array - ideas?

You can use NSSelectorFromString function. See here :
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSelectorFromString

Related

How to create Multiple buttons programmatically, get button name whenever press on particular button?

I want to create multiple buttons using MutableArray count. After creating the button, whenever pressing on particular button, I want to display that button name in a label. But Once press on button, it is always showing the MutableArray last index value name. Please help in this problem. I am fresher ti iOS. It's my code.
-(void)ViewDidLoad {
NSMutableArray *Mute_array=[[NSMutableArray alloc]initWithObjects:#"One", #"Two", #"Three", #"Four", #"Five", nil];
for (int k=0; k<[Mute_array count]; k++){
UIButton *btn_sub_category = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn_sub_category addTarget:self action:#selector(clicks:) forControlEvents:UIControlEventTouchDown];
[btn_sub_category setTitle:[Mute_Sub_Category objectAtIndex:k] forState:UIControlStateNormal];
btn_sub_category.frame = CGRectMake(278, k*130, 483, 44);
[self.view addSubview:btn_sub_category];
}
}
-(void)clicks:(id)sender{
label.text=[btn_sub_category currentTitle];
//Here a want to send the name of that clicked button current title
}
But, here button current title is always showing MutableArray last object "Five" only. I want pressed button current title.
-(void)clicks:(id)sender{
UIButton *theButton = (UIButton*)sender;
NSString *theButtonTitle = [theButton titleForState:UIControlStateNormal]
NSLog(#"theButtonTitle :- %#", theButtonTitle);
label.text=theButtonTitle;
//Here a want to send the name of that clicked button current title
}
Try this code.
I using following method to get title of button
titleForState:
Returns the title associated with the specified state.
- (NSString *)titleForState:(UIControlState)state Parameters
state
The state that uses the title. The possible values are described in UIControlState.
Return Value
The title for the specified state. If no title has been set for the
specific state, this method returns the title associated with the
UIControlStateNormal state.
Read Documentation https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/titleForState:
Try like this:
-(void) clicks:(id)sender
{
if ([sender isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton*)sender;
NSString *title = button.currentTitle;
// do whatever you want with title
}
}
Try Like,
-(void)clicks:(id)sender{
UIButton *resultebutton= (UIButton*)sender;
label.text=resultebutton.titleLabel.text;
}

How to increment a label value when click a button

In my iPad app.i want to increment a label value when click a button.
actually label values are sending from previous view. so now i clicked a button but the last label value was incremented. see the below picture.
in the above picture i want to increment the label value in between the minus(-) and plus(+) buttons. but when i clicked plus button in first view but the label value is incremented on the third view.
** the above three views shown on the picture are sub viewed the scroll view **
i'm using this code......
-(IBAction)plusbutton:(id)sender
{
val = [sender tag];
NSLog(#"the_tag %d",val);
itemref.countVal++;
[self createOrderView];
}
According to you
fist give same tag value for - and + button and label in that single view so when click on the button
in button action
NSArray *subviews=[self.scrollview subviews];
for(UIView *sb_local in subviews)
{
if(sb_local.tag==[sender tag])
{
if([sb_local isKindOfClass:[UILabel class]])
{
UILabel *new_label=(UILabel *)[sb_local viewWithTag:pictag];
new_label.text = #"your value";
//[new_label.text intValue]you get int value from that label.Increment or decrement that int value according your (+,-)button actions and assign again it to that label.
}
}
}
Please place this code in ibaction + and - buttons please give different actions for + and - buttons in each view

Radio Button implemetation with GSRadioButtonSetController not changing state in reference to color

I am using GSRadioButtonSetController for the radio button implementation...I created two buttons and included in IBOutletCollection(UIButton) NSArray *buttonCollection....then I included GSRadioButtonSetController.h and GSRadioButtonSetController.m file to my project. Then to my viewdidload I added
// Instantiate your GSRadioButtonSetController object
self.radioButtonSetController = [[GSRadioButtonSetController alloc] init];
// Set its delegate to your view controller
self.radioButtonSetController.delegate = self;
// Set its buttons property to an array of buttons that you've
// created previously.
self.radioButtonSetController.buttons = buttonCollection;
and then I put
- (void)radioButtonSetController:(GSRadioButtonSetController *)controller
didSelectButtonAtIndex:(NSUInteger)selectedIndex
{
//implemetation
}
There are tow image for the button...one for the select state and 2nd for the deselcetd state....the code is giving me indexes however I dont know how to change to image dynamically so that user know which one is selected and which one is not...
Thanx in advance
When you create the buttons to add to `buttonCollection', set image to buttons, like:
[button_1 setImage:[UIImage imageNamed:#"radio_button_off.png"] forState:UIControlStateNormal];
[button_1 setImage:[UIImage imageNamed:#"radio_button_on.png"] forState:UIControlStateSelected];
it worked for me

How to find out programmatically added textfields in a scrollview

Inside my scrollView i am adding uitextfields programmatically by clicking an "ADD" Button. the number of textfields depending on how much i press in add button. it can be 'N' number of textfields.
After this fields editing how can i get values inside the all textfields into an array.
if once i edited textfield there is a possibility to delete entry inside that. So what is the way to save values in the all text fields while clicking in Save button.
i am attaching my screenshot with this.
Thanks in Advance
use fast enumeration.
NSMutableArray *arr = [NSMutablearray....];
for (UIView *subV in self.view.subviews){
if([subV isKindOfClass:[UITextField class]]){
//store it in a NSDictionary, so later can still know which
//textField your text belongs,
NSDictionary *tempDic = [NSDictionary dictionaryWithObjectAndKey:subV.txt
,subV.tag,/*or subVw.placeholder*/,nil];
[arr addObject:tempDic];
}
}
You can get all the textfields by iterating through the subview array of the scrollview
for (UIView *subView in scrollview) {
//check if the subView is a textfield by for example matching a certain tag
//save text of textfield
}
When you are creating textField then assign tagValue to every textField and then access textField using tag value....
UITextField *txtField = (UITextField *) [scrollView viewWithTag:tag];

How to animate table view cells when switching between buttons of UISegmentedControl?

My application is looking like the below image
I have 3 buttons in my segmented control. Assume that hitting the "Department" button will display 3 departments & hitting "Name" button will show 6 names.
I follow the below steps to make this..
Create UISegmentedControl by this code
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Department", #"Name", #"ID",nil]]; //Initialize Segmented Control
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; //Define bar type (optional)
segmentedControl.selectedSegmentIndex = 0; //Define default clicked button
[segmentedControl addTarget:self action:#selector(segmentClicked) forControlEvents:UIControlEventValueChanged]; //Set target and define which method should called when tapping buttons
self.navigationItem.titleView = segmentedControl; //To add UISegmented Control in the title of the navigation bar
Define the method(segmentClicked) for UISegmented Control
segment = segmentedControl.selectedSegmentIndex;
if (segment==0) {
//Do something for Department tab
[self.tableView reloadData];
}
else if (segment==1) {
//Do something for Name tab
[self.tableView reloadData];
}
else {
//Do something for ID tab
[self.tableView reloadData];
}
Create the cells with reference to the value of segment in tableView: cellForRowAtIndexPath: method. (So that we can get various cells for each UISegmentedControl buttons)
if (segment == 0) {
cell.textLabel.text = [listOfDepartments objectAtIndex:indexPath.section]; //listOfDepartments is the NSMutableArray that contains list of Departments names
}
else if (segment == 1) {
cell.textLabel.text = [listOfNames objectAtIndex:indexPath.section]; //listOfNames is the NSMutableArray that contains list of person names
}
else {
cell.textLabel.text = [listOfIDs objectAtIndex:indexPath.section]; //listOfIDs is the NSMutableArray that contains list of person ID's
}
return cell;
So, finally i got the view showed in the above image.
When i press on the UISegmentedControl buttons, the table view reloads its cells & showing corresponding values perfectly. By default, the "Department" button was selected. If i tap the "Name" button(that is, table will show 6 cells instead of 3), The table changes immediately without any animations. It simply hide 3 cells of department names and showing 6 cells that displaying names of persons without any animations. I could not find a way to making the cell changes with smooth animations.
What should i implement to achieve the smooth animations?
Thanks in Advance
Use insertRowsAtIndexPaths:withRowAnimation: and deleteRowsAtIndexPaths:withRowAnimation: methods which has options for specifying some build-in cool animations.
typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
} UITableViewRowAnimation;
It may help to try:
[self.tableView beginUpdates];
[self.tableView endUpdates];
in your segmentClicked method, instead of [self.tableView reloadData];
These methods sometimes animate table changes better.
Use the method reloadSections: withRowAnimation:
If you are having only one section then pass [NSIndexSet indexSetWithIndex:0] as a first argument in above method. In the second argument you can choose the animation type.