I am currently working with a UISegmentedControl with 2 selections here. I have already defined it in -(void)viewDidLoad:
_segmentSelector = [[UISegmentedControl alloc] initWithItems:_selectorItems];
_segmentSelector.frame = CGRectMake(40, 356, 200, 43);
_segmentSelector.segmentedControlStyle = UISegmentedControlStyleBezeled;
_segmentSelector.selectedSegmentIndex = 0;
[_segmentSelector addTarget:self
action:#selector(selectorValueChanged)
forControlEvents:UIControlEventValueChanged];
The action for #selector(selectorValueChanged) is as following:
- (void)selectorValueChanged
{
if(_segmentSelector.selectedSegmentIndex=1)
{
}
if(_segmentSelector.selectedSegmentIndex=0)
{
}
}
My problem is, that when i run a simulation and clicked on one button, then attempted to click another button, the SegmentedControl does not respond. It stays on the selection and refuses to accept another tap/click/whatever action i ordered it to. What is wrong with it?
In selectorValueChanged, you assign new values to the selected segment index:
if (_segmentSelector.selectedSegmentIndex = 1)
Your probably mean
if (_segmentSelector.selectedSegmentIndex == 1)
Related
I am creating an app,which contains five buttons ,created programmatically.The following are the requirements...
while clicking the first button it stay highlighted.while clicking second button ,first become normal and second stays highlighted...ie the particular button clicked becomes highlighted ,all others remains normal.......please help..
You can set
[button setBackgroundImage:[UIImage imageNamed:#"normalbackgroundimage"] forState:UIControlStateNormal]; //not highlighted
[button setBackgroundImage:[UIImage imageNamed:#"highlightedbackgroundimage"] forState:UIControlStateHighlighted | UIControlStateSelected]; //highlighted
and now when you set your button.selected = YES it will get highlighted
so you could do something like this on button click
button1.selected = YES;
otherButton.selected = NO;
EDIT
Simple solution to your problem would be to create one ibaction connect it to all your buttons you want to selected/deselect and just do this
-(IBAction)someButtonPressed:(UIButton*)sender{
button1.selected = NO;
button2.selected = NO;
button3.selected = NO; //and so on...just set all your buttons to selected = NO
//at the end you just select button you clicked
sender.selected = YES;
}
This would be my solution to this:
Get a property to keep track of the selected button
#property (nonatomic, weak) UIBUtton *lastSelectedButton;
In your button callback method (the selector you configured when you added the buttons):
- (void)didClickButton:(UIButton *)button {
if (self.lastSelectedButton isEqual:button) {
// Don't need to do anything in this case because the button is already selected
return;
}
[self.lastSelectedButton setSelected:NO];
[button setSelected:YES];
[self setLastSelectedButton:button];
}
Let me know if you have questions or need more help!
You can create a instance variable int which holds a tag value. Now which ever button you click you need to clear the previous button's tag value and assign it to the current button.
For Ex:
int const TAG_HIGHLIGHTED_BUTTON = 100;
- (void)buttonAction:(UIButton *)button {
UIButton *prevButton = [self.view viewWithTag:TAG_HIGHLIGHTED_BUTTON];
[prevButton setTag:0];
[button setTag:TAG_HIGHLIGHTED_BUTTON];
}
I have this button code which is similar in two buttons (part of the code)
btn.frame=CGRectMake(600,400,30,30);]
btn.addTarget:self actionL#selector(authButtonAction)forControlEvents:UIControlEventTouchUpInside]
and in the function (dummy code)
-(void)authButtonAction {
if btn1 was clicked btn1.caption=y else btn1.caption=2
}
My button works and my function is called but what I was trying to do is to somehow find out which button was clicked...pass that to authButtonAction. Because that would save me from writing X amount of functions and I could use an if statement in one function.
Set a tag to your button..
#define TAG_BUTTON_ONE 1
#define TAG_BUTTON_TWO 2
oneButton = [[UIButton alloc] ......];
oneButton.tag = TAG_BUTTON_ONE;
[oneButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
-----
-----
twoButton = [[UIButton alloc] ....];
twoButton.tag = TAG_BUTTON_TWO;
[twoButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
---
--
In button handler, check for sender's tag
-(void) buttonClicked:(UIButton*)sender{
if(sender.tag == TAG_BUTTON_ONE){
//handle button one click
}else if(sender.tag == TAG_BUTTON_TWO){
//handle button 2 click
}
}
Set btn.Tag to an id for your button, then use that to determine which button was pressed.
Your button method should have a colon on the end of its name. The button will pass itself as the sender argument. You can query that to find out which button was clicked (based on its title or tag).
You can give tags to button and then can check tag of tappedButton in action method.
-(IBAction)ButtonPressed:(id)sender
{
UIButton *pressedButton = (UIButton *)sender;
int buttonTag = pressedButton.tag;
if (buttonTag==1)
{
NSLog(#"Button 1 Pressed");
}
else
{
NSLog (#"Button 2 Pressed");
}
}
I am loading a bunch of images into a UIScrollView, I then resize them and position a button over each, so when I click on the button over the smaller image, it will load up the full size image in a new view.
I am struggling to find a decent way to somehow pass the image's URL to the click handler as a parameter when the button is clicked, and from what I have read, passing parameters to callback functions is not possible.
Here is the code I have thus far,
for(Card *card in crd){
pocketImage = [self imageWithImage:[UIImage imageNamed:#"pocket.png"] scaledToSize:CGSizeMake(245/2,80/2)];
pocketImageView = [[UIImageView alloc] initWithImage:pocketImage];
cardImage = [self maskImage:[UIImage imageNamed:#"animal.jpg"]];
cardImageView = [[UIImageView alloc] initWithImage:cardImage];
y = (80/2) * inc;
if (inc % 2){
x = 125;
} else {
x = 10;
y += (80/2);
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(testMe) forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(x,y-40,pocketImageView.frame.size.width,pocketImageView.frame.size.height*2)];
[pocketImageView setFrame:CGRectMake(x,y,245/2,80/2)];
[cardImageView setFrame:CGRectMake(x,y-40,245/2,80/2)];
//add the pocketImageView to the container
[imageContainer addSubview:cardImageView];
[imageContainer addSubview:pocketImageView];
[imageContainer addSubview:btn];
inc++;
}
Callback function,
-(void)testMe {
NSLog(#"works");
}
So basically that just loops through all of the images, and then appends all of them to a UIScrollView, which works fine, the problem comes in when I try to pass custom parameters to the testMe method, which isn't possible as far as I know, so how would I be able to click on the button and then display the corresponding full size image in a new view?
You can use an iVar / (private) #property for this.
Just set the data you want to have available like this:
myTransferVariable = // something you need in you testMe method
and use it like this
-(void)testMe {
NSLog(#"%#", myTransferVariable);
}
You should be able to define your action as - (void)testMe:(id)sender { and specify it as #selector(testMe:). If you then give each button a tag that is (for example) an offset into an array of URL strings, the method should be able to find a URL that matches the selected button by using ((UIButton *)sender).tag.
I'm working on a script that loops through an array of LED UIImageView's that are setup in numerical order and are selected by tag. Depending on the step (aka number) it's on the led image is shown as on or off. The main goal of this method is to take the current step and display the "LED on" image while subtracting it by one and display the "LED off" image for the previous step. So, only one LED should be lit up at a time.
Unfortunately, I can only get the "LED on" image to display correctly. All of the LED's in the sequence light up, but they never turn off. My first guess is that I'm not subtracting the NSInterger in the right way. However, when I check the log, everything is where it should be. If the current step is 2, previous is 1. No idea why this isn't working. Thanks!
sequencerLocation and previousLocation are both setup as properties.
- (void)clockLoop:(UInt8)seqClockPulse
{
//cast to an int to use in loop
NSInteger stepCount = sequencerSteps;
//increment sequencer on pulse in
sequencerLocation++;
if(sequencerLocation > stepCount)
{
sequencerLocation = 1;
}
//setup previous step location
previousLocation = (sequencerLocation - 1);
if (previousLocation == 0)
{
previousLocation = stepCount;
}
//change led color in led array
for (UIImageView *led in sequencerLEDArray)
{
if(led.tag == sequencerLocation)
{
UIImageView *previousLed = (UIImageView *)[led viewWithTag:previousLocation];
[previousLed setImage:[UIImage imageNamed:#"images/seq_LED_off.png"]];
NSLog(#"Previous: %d", previousLocation);
UIImageView *currentLed = (UIImageView *)[led viewWithTag:sequencerLocation];
[currentLed setImage:[UIImage imageNamed:#"images/seq_LED_on.png"]];
NSLog(#"Current: %d", sequencerLocation);
}
}
}
//change led color in led array
for (UIImageView *led in sequencerLEDArray)
{
if(led.tag == sequencerLocation)
{
// I THINK the problem is here
// UIImageView *previousLed = (UIImageView *)[led viewWithTag:previousLocation];
// TRY THIS instead
UIImageView *previousLed = [led.superview viewWithTag:previousLocation];
[previousLed setImage:[UIImage imageNamed:#"images/seq_LED_off.png"]];
NSLog(#"Previous: %d", previousLocation);
// HERE you don't need to search for the tag you already tested for it in your if statement
UIImageView *currentLed = (UIImageView *)[led viewWithTag:sequencerLocation];
[currentLed setImage:[UIImage imageNamed:#"images/seq_LED_on.png"]];
NSLog(#"Current: %d", sequencerLocation);
}
}
viewWithTag:
Discussion
This method searches the current view and all of its subviews for the specified view.
So when you search for led by it's tag from itself, it's returning itself, but when you search its brother it's not finding it, that is why I'm suggesting the led.superview as the place to search for your tag, the parent should be able to find its other child.
i have an awkward problem where calling to [self invalidateModel]; does not redraw my custom cells.
I have a TTTableViewController with an action sheet that allows to order the display of a table when pressed. The data gets redrawn correctly but the cells are not redrawn. Instead there are reused.
To illustrate better :
In my - (void)layoutSubviews { of my CustomTableItemCell class i have this :
if (bookmarked) {
UIImage *bookmark = [UIImage imageNamed: #"bookmarked#2x.png"];
TTImageView *bookmarkView = [[TTImageView alloc] init];
bookmarkView.defaultImage = bookmark;
bookmarkView.frame = CGRectMake(297, 0, 16, 27);
[self.contentView addSubview:bookmarkView];
TT_RELEASE_SAFELY(bookmarkView);
}
Basically if I get a bookmarked item, I want to display a ribbon on the right of my cell. This ribbon gets display correctly.
When I reorder my cell with the action sheet method and call invalidateModel, the first cell which didn't have any ribon gets putten at a place where was previously a ribbonned item but without redrawing the cell, thus giving a ribon to an item without ribbon.
Code source :
This is my createDatasource function of my TTTableViewController :
- (void)createModel {
self.dataSource = [ServiceRequestDetailedDataSource viewDataSource:self.typeOfAction andOrderBy:orderBy];
// If dataSource nil, show an empty Message
if (self.dataSource == nil) {
[self showEmpty:YES];
}
}
This is my action sheet action of my TTTableViewController that change the orderBy and calls invalidate model :
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
self.orderBy = #"portfolio";
[self invalidateModel];
}
Any tips would be great, I am really stuck here :'(
Maybe [self reload] would help you update cells view. Call it after model invalidation
Found it, just use reuseidentifier. My bad.