I want to add total 5 button at bottom of screen but only four will be visible at first time loading of nib.
and if user press first button it will be disappear with animation and second button will come at place of first . and now in screen one more button (button 5 ) at last.
to make it more clear i am adding image.
screen 1. when nib loaded first time.
screen 2. when button 1 pressed.
screen 3. when button 2 pressed.
AS U HAVENT POSTED ANY CODE THAT U'VE TRIED.
So Here is some idea I can suggest to u.
If u want to disappear the button when pressed then, instead of removing it from the view u can keep it there but only make it invisible , may be by setting properties of "visual appearance of view"(look for UIView class reference) eg. alpha value,hidden property.
Once u invisible it, then make it visible after some delay eg.1/2 second.
Once button is visible then CHANGE the label text.
In case ur, u'll need to set UILabel text of all four button; by incrementing label-text int value by 1.
How about something like this? It's not perfect so you'll have to play with it but it should get you started in the right direction...
in your .h
IBOutlet UIButton *firstButton, *secondButton, *thirdButton, *fourthButton
NSArray *buttonsArray;
in your .m
-(void)viewDidLoad{
// VDL stuff
buttonsArray = [NSArray arrayWithObjects:#"One", #"Two", #"Three", #"Four", #"Five", #"Six", nil];
[firstButton setTitle:[buttonsArray objectAtIndex:0]];
[firstButton setTag:0];
// initial setup of the other three buttons works the same way
}
-(IBAction)updateButtons:(id)sender{
UIButton *btn = (UIButton*)sender;
int index = btn.tag;
for(int i = 0; i <4; i++){
if([buttonsArray count] > index+i){
switch(i){
case 0:;
[firstButton setTitle:[buttonsArray objectAtIndex:i+index]];
[firstButton setTag:i+index];
break;
case 1:; //secondButton
break;
case 2:; //thirdButton
break;
case 3:; //fourthButton
break;
}
}else{
break;
}
}
}
Then all you need to do is connect the four on-screen buttons to the action and in the else you'll need some logic (pretty much the same as the switch-case above) to hide ones once you get towards the end of the array if that's what you want to do.
Related
OK, so this one might be a little difficult to explain.
I am trying to make an app in objective c were on the first screen you can slide between pictures that describe pages, but are not actually the page. And when you click a picture, you go to the relevant page the picture presented.
I used a very nice youtube tutorial for using the slide option with ScrollView and PageControl.
According to this tutorial you make a new class, successor of UIView named "Slide" with h., m. and xib file.
In your MainViewController you have an array of "Slide" and you can swap between Slides.
This is the creation of the array:
- (NSArray*)createSlides{
NSArray *arr;
Slide *slide1 = (Slide*)[[NSBundle mainBundle] loadNibNamed:#"Slide" owner:self options:nil].firstObject;
slide1.label.text = #"Slide1";
[slide1.button setBackgroundImage:[UIImage imageNamed:#"scratch"] forState: UIControlStateNormal];
Slide *slide2 = (Slide*)[[NSBundle mainBundle] loadNibNamed:#"Slide" owner:self options:nil].firstObject;
slide2.label.text = #"Slide2";
[slide2.button setBackgroundImage:[UIImage imageNamed:#"expose"] forState: UIControlStateNormal];
arr = [NSArray arrayWithObjects: slide1, slide2, nil];
return arr;
}
Than in another function I use this:
for (int i = 0; i<slides.count; ++i) {
slides[i].frame = CGRectMake(width * (CGFloat)i, 0, width, height);
[_slideScrollView addSubview:slides[i]];
}
(This is not all of it, but I'm not sure the other parts of the code are relevant for my question.).
So far so good. It works perfectly.
Now, I want to add the functionality of clicking the picture and perform segue to the relevant view controller. For example, I have "ExposeViewController" for slide2.
The problem is that I click the picture on "Slide" but I perform the segue from the MainViewController.
At first I tried to make UIButton on Slide class (that shows the relevant picture) and to create a button action which somehow detect the Slide's (UIView's) ViewController, pass it somehow the name of the segue identifier and than in a method of the view controller to perform the segue.
It didn't work and I didn't like this solution.
After few hours of research i'm still not sure how to make it.
How can I make user press on button that is part of UIView (Slide) perform method in it's view controller to go to another view controller?
Just provide the different tag to each button like this.
Slide1.button.tag = 1
Slide2.button.tag = 2
Then add same custom action to each button like
[Slide1.button addTarget:self action:#selector(functionName:) forControlEvents:UIControlEventTouchUpInside];
[Slide2.button addTarget:self action:#selector(functionName:) forControlEvents:UIControlEventTouchUpInside];
Now we create the selector function manually like this-
-(void) functionName:(UIButton*) sender{
If (sender.tag == 1){
[self performSegueWithIdentifier:#"//firstIdentifier" sender:self]; }
else If (sender.tag == 2) {
[self performSegueWithIdentifier:#"//secondIdentifier" sender:self]; }
}
Where you need to check the particular tag of Sender button and perform the action accordingly.
I want to have a UISegmentedControl with a number of selectable options in it, but also include an Info button, which is not selectable. The UISegmentedControl class reference talks about using a momentary disclosure button, hinting that its possible, but there is nothing else there or in the header file that explains how to do such a thing.
So how can I do this?
After much experimentation, I found that keeping a shadow variable on the selected index let me do this. In the following code, the info button is 1, the other buttons are selectable. In viewDidLoad set the initial value (or ask the control for its selected index). Then use the following code for the action method:
- (IBAction)segmentAction:(UISegmentedControl *)sender
{
NSUInteger idx = [sender selectedSegmentIndex];
switch(idx) {
case 1: // momentary button
sender.selectedSegmentIndex = selectedIndex;
break;
default:
selectedIndex = idx;
// other stuff
break;
}
...
Essentially all you need to do is reset the selected index. Tested with iOS7 only.
I have an NSMutableArray of buttons. When one of the buttons is pressed, I change it to a darker color. But I also want to change the background color of the other buttons so none of the previously pressed keep the darker color.
for(UIButton* u in menuButtons)
{
NSLog(#"test");
[u setBackgroundColor:[self byteColor:141.0 g:52.0 b:42.0] ];
}
But it doesn't even go into this loop. What should I be doing differently?
it doesn't even go into this loop means the buttons are not added properly to the array.Initialize the array and add the outlets first properly and the method will work
I have 15 UIButton in my iPhone project, after clicking on button one image is pop up and all images are different for different buttons, how could I achieve this by using touch?
How should I call proper image for particular button?
I dont want to take 15 UIButtons and 15 UIImageView .
You could have all 15 buttons hooked to one single "IBAction" method.
When the method is called, the parameter that is sent (e.g. "(id) sender") is the object that sent the message.
If you assign different tags to each button, you'll know which button called the action method and then you could have a different image appear for each different button.
All you need here is one "IBAction" method and one "IBOutlet" for a single image view. You will still need to have some kind of logic to load 15 different images into that single image view, though.
Here is how you can do:
This is called after any button is clicked:
-(IBAction)buttonClicked:(UIButton *)sender {
if(sender.tag == 0){
NSLog(#"Button one");
} else if(sender.tag == 1){
NSLog(#"Button two"); ... // like wise do it for 15 buttons
}
}
All you have to do is assigne tag to the button when you create it.
I think you must be running a for loop 15 times to create those buttons,
so:
for(i=0;i<15;i++){
//create button..
button.tag = i;
}
Have a background image of the view with all the 15 button information, which is divided to 15 locations to detect the touches.
Get the touch location (co-ordinates) and based on the particular location that is pre defined, update the image in the popup UIImageView accordingly.
this might help to get the location of the touch in the view.
UIView touch location coordinates
You could use IBOutletCollections, this requires that you hook up the 15 UIButtons in a Xib.
The following code assumes you have a collection of N-buttons. For a given gesture, it will iterate over the buttons in the collection and determine if the location of the touch is within a buttons frame.
Edit: IBOutletCollections are available in iOS 4 and up.
Assuming you have the following as properties:
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *collection_buttons;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
And the following synthesizes:
#synthesize collection_buttons = _collection_buttons, imageView = _imageView;
Then you could use the following code on gesture call back.
-(IBAction) tapGestureOnButton:(UITapGestureRecognizer *) gesture {
CGPoint point = [gesture locationInView:self.view_buttonContainer];
for( int index = 0; index < self.collection_buttons.count; index++ ) {
if( CGRectContainsPoint(((UIButton *)[_collection_buttons objectAtIndex:index]).frame, point)) {
//Button at index has been pressed
[imageView setImage:[button backgroundImageForState:UIControlStateNormal]];
break;
}
}
}
I am making an app for iPad with different views. Till now I have added 2 views. First view is the home screen which has a single button and some images. Pressing button on first view navigates to the second view. Second view has 6 buttons. Now what I want to do is when I press any button on second view, it navigates to the third view. Each button on second view has to show different data. I don't want 6 different views, instead I want a single third view but it should show only that particular data respective to the button pressed on second view.
How can it be done?? Please help me with the code..
Any help will be highly appreciated..
You just need a variable in the third view that you set before showing it. I would set a different tag value for each of my buttons and have them all call a single method. In that method, check the tag value of the sender and setup the third view accordingly. Then show it.
In the third view's viewDidLoad method you can handle displaying or setting up the new data you assigned to it.
For example, if you were setting some custom text in the third view you would have this for your button method in the second view:
- (IBAction)buttonTap:(id)sender {
UIButton *tappedButton = (UIButton *)sender;
MyThirdViewController *thirdVC = [[MyThirdViewController alloc] initWithNib:#"MyThirdViewController" bundle:nil];
switch (tappedButton.tag) {
case 1:
thirdVC.customText = #"Something for button 1";
break;
case 2:
thirdVC.customText = #"Something for button 2";
break;
case 3:
thirdVC.customText = #"Something for button 3";
break;
}
[self.navigationController pushViewController: thirdVC];
[thirdVC release];
}
In the third ViewController:
- (void)viewDidLoad {
self.myTextView.text = self.customText;
}