Sorry for the noob question. I have a view that has a UISegmentControl in the UIToolBar. I also present a popover from a button. How do I get the value of the UISegmentControl to the popover? Should I have the Popover have an NSInteger ivar to hold this value so when I present the popover, I set that value to whatever the selectedSegmentIndex is? I didn't know if that was the cleanest way since I'm new at this and keep reading stuff about not coupling your classes. Thanks!
In my opinion , You can use UIControlEventValueChanged Event of UISegmentControl to directly set the TAG Propertly of popover to the selectedSegmentIndex of UISegmentControl .
{
[segmentedControl addTarget:self
action:#selector(setvalueofselected:)
forControlEvents:UIControlEventValueChanged];
}
-(IBAction)setvalueofselected:(id)sender
{
popover.tag = segmentedControl.selectedSegmentIndex;
OR
self.intSelected = segmentedControl.selectedSegmentIndex;
}
And from with in your popover click event code try to check the property of TAG . You can also use Some Global Integar for this purpose instead of tag property.
In this way your POPOver can know which segment was selected when they clicked popover.
Related
you show this image
i need add button in 7 item to last in table view.
and i need every button's different selector.
this is possible?
EDIT:
I need code that idea.
Yes ofcourse ,keep condition in cellForRowAtIndexPath method, indexPath.row>6 then add button otherwise left out.
Give tag value to the button and based on that tag value perform the different operations whatever you want.
place this one in cellForRowAtIndexPath
if(indexPath.row>6){
UIButton *checkBtnOne= [UIButton buttonWithType:UIButtonTypeCustom];
checkBtnOne.frame=CGRectMake(10, 76, 21, 21);
checkBtnOne.tag=indexPath.row;
checkBtnOne.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"btn_radio.png"]];
[checkBtnOne addTarget:self action:#selector(radioButton:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:checkBtnOne];
}
-(IBAction)radioButton:(id)sender{
UIButton *checkBtn=(UIButton *)sender;
// here you will get the tag value so based on that you can perform the action.
if(checkBtn.tag==6)
//first button
else if (checkBtn.tag==7)
//second button
else if (checkBtn.tag==8)
//third button
.
.
.
.
}
You can implement two sections for the UITableView.
In first one you should use default cells with custom background
image.
In second section you can use Custom subclass of UITableViewCell (With UIButton in it)
It will make your code cleaner.
For actions specific to second section's cells you can implement it using #Sunny's answer.
In Cell for row at index path method of UITable View
check index path.row > 6
Add Button and set button object.tag = index path.row.
Thus you will find each button working individual with their tag value.
Don't forget to add target for added button and recieve tag value in added method for use button individually.
I'm subclassing UITextField and want to assign to its rightView property a custom button.
I setup the button in setFrame of the subclass.
My textfields had their outlet connections in the ViewController and Class set to my subclass in Identity Inspector in Interface Builder.
The problem is that my custom button is not appearing.
I've tried to overcome the problem and made a setupRightView method in my subclass.
Then, when I call setupRightView on the outlet property from the View Controller - voila - everything is working just fine!
I'm fairly new to Objective-C and I suppose I'm making some stupid mistake with the object model.
Why I can't set correctly rightView property in my subclass?
Did you remember to do this:
self.rightViewMode = UITextFieldViewModeAlways;
That property is set to UITextFieldViewModeNever by default, so you won't see your button if you don't include that line.
After Edit: I checked on putting my code in setFrame, and it didn't work there -- not sure why. It worked fine if I put it in awakeFromNib.
-(void)awakeFromNib {
UIButton *myButton = [UIButton buttonWithType:2];
self.rightView = myButton;
self.rightViewMode = UITextFieldViewModeAlways;
}
I have a custom table view cell view that contains a button. I initialize the button to setBackgroundImage to an empty circle in its normal state. I also setImage to a checkmark image for its selected state.
checkmarkButton = [[UIButton alloc] initWithFrame:CGRectMake(kLeftMargin, kTopMargin, kButtonSize, kButtonSize)];
[checkmarkButton setBackgroundImage:[UIImage imageNamed:#"empty-circle.png"] forState:UIControlStateNormal];
[checkmarkButton setImage:[UIImage imageNamed:#"checkmark.png"] forState:UIControlStateSelected];
[checkmarkButton addTarget:self action:#selector(checkmarkButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:checkmarkButton];
When the button is clicked on, I set the button as selected and told to redraw so that the button looks like a circle with a checkmark in it. Click on it again and selected is set to NO and the cell is told to redraw so that it's an empty circle.
- (void)checkmarkButtonPressed:(id)sender
{
[checkmarkButton setSelected:!checkmarkButton.selected];
[managedObjectContext save:nil];
}
My problem is that this button exists in a custom table view cell view, which seems to control how it's drawn in a way that I can't identify. When I long-click (touch down in the cell and staying in the cell without touching up for a while) on a cell whose button is set selected (should display a checkmark in a circle), the checkmark disappears until I touch up. When I touch up, the state of the cell is correct. It's just wrong while I am long-clicking the cell.
So, how do I control how that button is drawn when I long-click on the cell?
I'm not sure if this is exactly your case because I didn't see your sources. But please check the Highlighted state of your button. When UIButton is placed on UITableViewCell then table controls your UIButton: on cell touch down (long touch) UIButton as well as UITableViewCell change its state to Highlighted and returns to Default or Selected state on touch up.
UPDATE:
I agree it is unexpected behavior. You can create subclass of UIButton and reimplement this call to leave it empty to prevent calling super method:
- (void)setHighlighted:(BOOL)highlighted;
Example:
#interface XButton : UIButton
#end
#implementation XButton
- (void)setHighlighted:(BOOL)highlighted { }
#end
This should prevent state changing by table.
when you click and hold a button the state that is getting called is the highlighted state, thus when you release the click the state returns to UIControlStateNormal
I have a UITableViewController, when there is no data to populate the UITableView, I want to add a button, which uses an image. So, rather than the user seeing a tableview with no records, they will see an image that says, "No records have been added, Tap to add one", then they click and we create a new one.
I assumed I would just hide the UITableView, then create the button, but I never see the button. Here I am using:
if ([[fetchedResultsController sections] count] == 0) {
self.tableView.hidden = YES;
// Create button w/ image
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setImage:[UIImage imageNamed:#"no-rides.png"] forState:UIControlStateNormal];
[self.view addSubview:btn];
}
Ideas on why I would never see the button? When I show this view, it seems to have a transparent background for a second, then changes white...
I am not sure if it works but you can try this:
If your view controller is not a UITableViewController and it contains a UITableView
[self.tableView removeFromSuperview]; then [self.view addSubview];
If your view controller is a UITableViewController, you may need to consider to set the first row to contain the image and text. Then, you can handle the event: tableView:didSelectRowAtIndexPath: and if user click on the first cell, you trigger the method handle the button event
In a UITableViewController, self.view could be self.tableView in which case hiding the table would also hide the button. Try using a custom UIViewController and creating either the table or the button as a subview of self.view instead.
Alternately, when there is no data, you can create a single custom cell containing your button and use that instead of normal cells.
I believe you can add the button to the table footer, as the table footer is shown even when there are no cells. (Note, this is different to a section footer.)
By default the tableFooterView property of the UITableView is nil. So just create your button, and then do:
self.tableView.tableFooterview = btn;
For all future travelers, I simply set .userInteraction = true (Swift) and it worked like a charm. All in all:
tableView.backgroundView = constructMyViewWithButtons()
tableView.backgroundView!.userInteraction = true
Is it possible to set up a UITextField with a leftView so that if a user clicks into the UITextField the keyboard shows but if they click on an icon in the leftView another method is called (i.e., one that displays a UIPickerview)?
Have you checked that the leftFieldViewMode is set to something other than never
[textField setLeftViewMode:UITextFieldViewModeAlways];
Instantiate a button as u do normaly
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,0,30,30)];
[btn setTitle:#"title" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(BtnClick) forControlEvents:UIControlEventTouchUpInside];
Instantiate a textfield as you do normally
UITextField *txtfield1=[[UITextField alloc]initWithFrame:CGRectMake(10, 10,300,30)];
[txtfield1 setBackgroundColor:[UIColor grayColor]];
[txtfield1 setPlaceholder:#" Enter"];
To set a view in left side(there is right view also available)
[txtfield1 setLeftView:btn];
if u want the view to show always then below code does it
[txtfield1 setLeftViewMode:UITextFieldViewModeAlways];
if u want the view to show never then
[txtfield1 setLeftViewMode:UITextFieldViewModeNever];
if u want the view to show if the textfield is not edited then below
[txtfield1 setLeftViewMode:UITextFieldViewModeUnlessEditing];
if u want the view to show if the textfield is edited then
[txtfield1 setLeftViewMode:UITextFieldViewModeWhileEditing];
button click event and pop over
-(void)BtnClick
{
//Usual pop over coding goes here
}
Have you tried using a UIButton for this task? From the documentation for the leftView property:
If your overlay view does not overlap
any other sibling views, it receives
touch events like any other view. If
you specify a control for your view,
the control tracks and sends actions
as usual. If an overlay view overlaps
the clear button, however, the clear
button always takes precedence in
receiving events.
So create a UIButton instance, configure its appearance and actions as needed, then set it as the leftView property.