I'm trying to use a Segmentcontroller.
When a button is press my callback gets call,
but when i use .selectedSegmentIndex to see what button was press, it returns a zero.
code
-(IBAction)changeSeg{
int g;
g=Segment.selectedSegmentIndex;
if(Segment.selectedSegmentIndex == 0){
UpperLower=0;
}
if(Segment.selectedSegmentIndex == 1){
UpperLower=1;
}
}
I think the method should look like this
- (IBAction)ChangeSeg:(UISegmentedControl *)sender {
if(sender.selectedSegmentIndex == 0){
UpperLower=0;
} else if(sender.selectedSegmentIndex == 1){
UpperLower=1;
}
}
The sender will have the selectedSegmentIndex that was chosen.
Related
I'm using two buttons to do the same action. How can I identify which button was pressed using the "sender" from my IBAction function? (each button has a different Tag)
Here is what I want to do:
- (IBAction)addItemInParent:(id)sender {
NSInteger choice = sender.tag;
}
I hope this question makes sense!
- (IBAction)addItemInParent:(id)sender
{
UIButton *buttonPressed = (UIButton *)sender;
if (buttonPressed.tag == 123)
{
//button with tag 123 was pressed
}else if (buttonPressed.tag == 124
{
//button with tag 124 was pressed
}
}
Just replace my 123 and 124 with what the button tags actually are.
If you are confident that this method is only invoked by a button press, then you can rewrite your method to this one:
- (IBAction)addItemInParent:(UIButton *)sender {
NSInteger choice = sender.tag;
}
If not, check for the class of the sender:
- (IBAction)addItemInParent:(id)sender {
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)sender;
NSInteger choice = button.tag;
}
else {
// handle other cases
}
}
Change id to UIButton like so:
- (IBAction)addItemInParent:(UIButton *)sender {
NSInteger choice = sender.tag;
}
After casting sender to UIButton, did you try:
if (choice == 101)
{
// button whatever was pressed
}
else
{
// the other button was pressed
}
Obviously you should use the tags you assigned to the buttons.
You need to
1) check if the id object is a actually UIView class or subclass
2) typecast the id object to UIView
3) check the tag
4) decide what next based on the tag
if ([sender isKindOfClass:([UIView class])])
{
NSInteger tag = ((UIView *)sender).tag;
if (tag == <tag of button 1>)
{
//do something
}
else if (tag == <tag of button 2>)
{
//do something else
}
}
You need to typecast because id can be any type of object and you must make sure that it is actually an instance of UIView class or subclass to be able to access the tag, as only UIView classes and subclasses have a tag property.
The below code for the most part works when selecting items, the bar button enables, but as soon as I unselect 1 with say 3 still selected, it disables.
How can I use the below code to disable when the count reaches 0 items selected?
- (void)assetsTableViewCell:(WSAssetsTableViewCell *)cell didSelectAsset:(BOOL)selected atColumn:(NSUInteger)column
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Calculate the index of the corresponding asset.
NSUInteger assetIndex = indexPath.row * self.assetsPerRow + column;
WSAssetWrapper *assetWrapper = [self.fetchedAssets objectAtIndex:assetIndex];
assetWrapper.selected = selected;
// Update the state object's selectedAssets.
[self.assetPickerState changeSelectionState:selected forAsset:assetWrapper.asset];
// Update navigation bar with selected count and limit variables
dispatch_async(dispatch_get_main_queue(), ^{
if (self.assetPickerState.selectionLimit) {
self.navigationItem.title = [NSString stringWithFormat:#"%# (%lu/%ld)", [self.assetsGroup valueForProperty:ALAssetsGroupPropertyName], (unsigned long)self.assetPickerState.selectedCount, (long)self.assetPickerState.selectionLimit];
}
});
}
Below is what needs adjusting.
if (selected == 1) {
self.navigationItem.rightBarButtonItem.enabled = YES;
} else if (selected == 0) {
self.navigationItem.rightBarButtonItem.enabled = NO;
}
selected will give you the state of the current asset so it wouldn't be wise to check this.
We need to check for some kinda global thing; basically to check for previous selections.
Looking at your navigationItem.title, it seems assetPickerState.selectedCount should do the trick.
So... maybe this?? (not sure but anyways...)
if (self.assetPickerState.selectedCount == 0) {
self.navigationItem.rightBarButtonItem.enabled = NO;
}
else {
self.navigationItem.rightBarButtonItem.enabled = YES;
}
Looks like you're only explicitly enabling the button when you have 1 selected (not more than that). Just going out on a whim but this might work (only disable if zero, otherwise enable it).
if (selected == 0) {
self.navigationItem.rightBarButtonItem.enabled = NO;
} else {
self.navigationItem.rightBarButtonItem.enabled = YES;
}
I have a simple application where the user inputs information into a series of UITextFields and a few other components in order for the app to populate with information.
I have 4 UITextFields (Name, Event, Subevent and Amount), 1 UITextView (notes), a UIDatePicker and a UISegmentedControl (Status).
The Name, Event, Amount and Status are required, where the Subevent and Notes are optional.
I've put some logic to disable the "Save" BarButtonItem if the the required fields are not filled in; however I'm noticing my logic is not working correctly.
Here's the logic:
- (void) checkTextFields
{
if (([self.nameTextField.text length] != 0 && [self.itemTextField.text length] != 0 && [self.occasionTextField.text length] != 0 && [self.isReceivedSegment selectedSegmentIndex] == 0) || [self.isReceivedSegment selectedSegmentIndex] == 1)
{
NSLog(#"This shouldn't be run if not every field is filled in");
self.saveButton.enabled = TRUE;
}
else
{
NSLog(#"Run");
self.saveButton.enabled = FALSE;
}
}
I'm calling this from the viewDidLoad:
self.saveButton.enabled = FALSE;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkTextFields) name:UITextFieldTextDidChangeNotification object:nil];
[self.isReceivedSegment addTarget:self action:#selector(checkTextFields) forControlEvents:UIControlEventValueChanged];
Problem
The problem is: if I enter the Name, Event and Select a Segment in the SegmentedControl, it SHOULD NOT enable the Save button because the Amount is not filled in.
The same applies if I put in the Name, Amount and Select a Segment, the Save button should not be filled in because the Event is not filled in, yet, it is in both of those situations.
Am I missing something obvious?
Try this:
- (void) checkTextFields
{
if ([self.nameTextField.text length] != 0 && [self.itemTextField.text length] != 0 && [self.occasionTextField.text length] != 0 && ([self.isReceivedSegment selectedSegmentIndex] == 0 || [self.isReceivedSegment selectedSegmentIndex] == 1))
{
NSLog(#"This shouldn't be run if not every field is filled in");
self.saveButton.enabled = TRUE;
}
else
{
NSLog(#"Run");
self.saveButton.enabled = FALSE;
}
}
The way you have implemented your conditions, with the parentheses, it will evaluate true when your selectedSegmentIndex = 1, regardless of the other conditions. Moving around the parentheses like so should work.
Notice that I have moved the parentheses to surround the OR portion of your conditions.
EDIT add these lines to your view did load method to have the form be checked after editing any text field.
[nameTextField addTarget:self action:#selector(checkTextFields) forControlEvents:UIControlEventEditingChanged];
[itemTextField addTarget:self action:#selector(checkTextFields) forControlEvents:UIControlEventEditingChanged];
[occasionTextField addTarget:self action:#selector(checkTextFields) forControlEvents:UIControlEventEditingChanged];
I have a view with two text fields and two segmented controls. I want each text field to become editable when its corresponding segmented control is selected.
Here is the approach I'm using:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == _textField) {
if (_segmentedControl.selectedSegmentIndex == 0 ||
_segmentedControl.selectedSegmentIndex == 1 ||
_segmentedControl.selectedSegmentIndex == 2 ||
_segmentedControl.selectedSegmentIndex == 3) {
return YES;
} else {
return NO;
}
if (textField == _textFieldTwo) {
if (_segmentedControlTwo.selectedSegmentIndex == 0 ||
_segmentedControlTwo.selectedSegmentIndex == 1) {
return YES;
} else {
return NO;
}
}
}
}
This works for the first segmented control and textfield, but the second text field remains editable regardless of the condition of the second segmented control.
Can anyone tell me where I'm going wrong?
Thanks
The IFs are not nested correctly.
if (textField == _One){
if( 0<= selectedIndex < 4) {
//do something
} else {
return no;
}
if (textField == _Two){
// other stuff
}
}
Can you see that the if(textFieldTwo) is inside the if(textFieldOne) ?
Looks like a simple logic error to me. Your:
if (textField == _textFieldTwo) {
should really be an else if on the first if.
Simply change it to:
} else if (textField == _textFieldTwo) {
and it should start working.
In my xib I have taken 4 UIbuttons named button 1, 2, 3 and 4. These four buttons are connected two four different IBAction methods which perform different functions.
Now I have one more button called "Save" This has also a different IBAction method.
- (IBAction)Save:(id)sender
{
}
Now here I want to check which of the above 4 UIButtons have been clicked.
For this I tried checking this way
- (IBAction)Save:(id)sender
{
if(sender == button1)
{
//Do this
}
else if (sender == button2)
{
//Do this
}
}
But this is not working. I am doing something wrong.Please help me out
Regards
Ranjit.
You can set the tag values for each button in the interface builder and set actions of all buttons to this method
//set global variable flag.
int flag;
- (IBAction)buttonClicked:(id)sender
{
switch ([sender tag])
{
case 0:
{
flag =0;
// implement action for first button
}
break;
case 1:
{
flag =1;
// implement action for second button
}
break;
case 2:
{
flag =2;
// implement action for third button
}
break;
//so on
default:
break;
}
}
for save button
- (IBAction)save:(id)sender
{
switch (flag)
{
case 0:
{
// first button clicked
}
break;
case 1:
{
// second button clicked
}
break;
case 2:
{
// third button clicked
}
break;
//so on
default:
break;
}
}
Define a class level ivar as
UIButton *selectedBtn;
Then in you IBActions
- (IBAction)button1:(id)sender {
selectedBtn = sender // or button1
}
- (IBAction)button2:(id)sender {
selectedBtn = sender // or button2
}
- (IBAction)button3:(id)sender {
selectedBtn = sender // or button3
}
- (IBAction)button4:(id)sender {
selectedBtn = sender // or button4
}
- (IBAction)Save:(id)sender
{
//Check output of below statement to ensure you're getting a sender
NSLog(#"Sender: %#", sender);
if(selectedBtn == button1)
{
NSLog(#"Button 1 pressed");
//Do this
}
else if (selectedBtn == button2)
{
NSLog(#"Button 2 pressed");
//Do this
}
else if (selectedBtn == button3)
{
NSLog(#"Button 3 pressed");
//Do this
}
else if (selectedBtn == button4)
{
NSLog(#"Button 4 pressed");
//Do this
}
}
Can you try this:
- (IBAction)Save:(id)sender
{
UIButton *pressedButton = (UIButton*)sender;
//Check output of below statement to ensure you're getting a sender
NSLog(#"Sender: %#", sender);
if([pressedButton isEqual:button1])
{
NSLog(#"Button 1 pressed");
//Do this
}
else if ([pressedButton isEqual:button2])
{
NSLog(#"Button 2 pressed");
//Do this
}
}
In your save method, check the Selected property of the other 4 buttons. If you do not want to keep the buttons in a selected state, but just want to see if they were clicked at some point, then define a property (e.g. an array) to track which buttons were clicked during the session, and check this property in your save method.