Change image when button is pressed - ios

In the app I have six buttons and when any of these buttons are pressed I want the image in the center of the screen to change to a different image but just for the time that any of the six buttons are being pressed. I am really struggling with this and all help would be appreciated. I am using a .xib file. Thanks again guys I am tearing my hair out with exasperation.
Thanks!

You need to have a responder(s) in your viewcontroller that is linked to all the button(s) on the viewcontroller through interface builder. In IB, to link a button to a method make sure the method is in your viewcontroller.h file and also implemented in the .m file. Then go to the connections panel in IB (after clicking on the button) and drag a connection from 'Touch Up Inside' (touch up inside is when a user touches the button and releases that touch up and inside the button's boundary) to the viewcontroller.xib window onto the file's owner icon. From there you will be presented with a drop down menu that will let you choose the IBAction methods you specified in the header file.
In this case take:
- (IBAction) buttonPressed: (id) sender; // header file
- (IBAction) buttonPressed: (id) sender { // .m file
// when the button is pressed then change the uiimageview
[imageView image: [UIImage imageWithContentsOfFile: #"image.png"]];
}

Related

How to make an animated button in Apple WatchKit?

I'd like to make a button with animation in WatchKit, but it seems I can't find a way to modify WKInterfaceButton or drag an image into button in storyboard. Any help is appreciated.
After some research I found the solution, it's pretty simple but easily ignored :)
First, drag a button into a scene created in storyboard.
Second, select button, modify its property content from Text to Group. If you can't find content property, click the Attributes Inspector button at top right of your screen, it looks like a breakpoint button or a down arrow with a bar.
Now you can control group created inside your button. You need to add a reference of this WKInterfaceGroup inside your controller code. Here is an example:
#interface AAPLButtonDetailController()
#property (weak, nonatomic) IBOutlet WKInterfaceGroup *buttonGroup;
#end
#implementation AAPLButtonDetailController
- (instancetype)init {
self = [super init];
if (self) {
// Initialize variables here.
// Configure interface objects here.
[_buttonGroup setBackgroundImageNamed:#"Bus"];
[_buttonGroup startAnimating];
}
return self;
}
So the button animation will be played after scene initialized. Remember only frame animation is supported for now, all frames in one animation should be named like "Bus0.png", "Bus1.png"....
Hope this can help :)
Reck's post worked for me in instances that I needed to control the repeat count or duration. If you simply want to start or stop an animation for a button, the following works for me, where twoButton is a WKInterfaceButton and I have a set of images name Bus0#2x.png, Bus1#2x.png, etc.
- (void)startButtonAnimating
{
// This works, but can't control repeat counts or duration
[self.twoButton setBackgroundImageNamed:#"Bus"];
// But you can stop after a delay by setting background image nil
[self performSelector:#selector(stopGroupButtonAnimating) withObject:nil afterDelay:1.5];
}
- (void)stopButtonAnimating
{
[self.twoButton setBackgroundImage:nil];
}

Making button click different options within UIWebView in Xcode

I have a view controller set up with a tab bar and webView. I need to make it like that when I press the "ATC" button in the bottom of the webView, it will choose the size that the user puts in the first page, then click the "Add To Cart" button on the website. Please let me know if you need more info. Thanks in advance.
Images in dropbox links below
https://www.dropbox.com/s/he0v3m39214a6qj/Screen%20Shot%202014-09-17%20at%209.44.33%20PM.png?dl=0
https://www.dropbox.com/s/3fuvbrixlo6y5ox/Screen%20Shot%202014-09-17%20at%209.44.41%20PM.png?dl=0
In .h file do like this
#property (retain, nonatomic) IBOutlet UIButton *btnATC;
- (IBAction)btnATCPressed:(id)sender;
In .m file viewDidLoad do like this
self.btnATC.hidden = NO;
and ask size for your shoe then hide your button
- (IBAction)btnATCPressed:(id)sender
{
// ask size for your shoe ......
self.btnATC.hidden = YES;
}

iOS - Right clicking on a View Controller in a Storyboard opens a menu, what does "manual" under Triggered Segues do?

iOS - Right clicking on a View Controller in a Storyboard opens a menu, what does "manual" under Triggered Segues do?
How do you use it? I can't seem to find any info in the Class Reference.
PS. The answer might be obvious, but I haven't figured it out. Be gentle!
if you want to present a viewController by pressing a UIButton or an UITableViewCell
you nee to hold the key Control and Drag with your mouse from the button or UITableViewCell to the destination viewController, once the destination viewController turns to blue color release the mouse button and a popup should appear with the title Action Segue, select one of the segues and that's it, now you can access to your viewController without any line of code
the UIButton and the UITableViewCell works when the user taps on it but if you have a object that don't have the same behavior of the button like a UIImageView then the Action Segue won't be available
for that kind of situations you need to make a Manual Segue
inside storyboard hold the key Control and Drag with your mouse from the viewController to the destination viewController, once the destination viewController turns to blue color release the mouse button and a popup should appear with the title Manual Segue, select one of the segues and now you should see a line with an arrow and a circle in the middle, click the circle and open the Attributes Inspector and add an identifier.
At this point you are done with storyboard now you need to add some code to your view controller to trigger that segue.
let's say that you have a UIImageView then you need to do the following.
- (void)viewDidLoad {
UIImageView *overflow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"my_image.jpg"]];
overflow.frame = CGRectMake(200, 200, 150, 150);
overflow.userInteractionEnabled = true;
[self.view addSubview:overflow];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[overflow addGestureRecognizer:tap];
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
[self performSegueWithIdentifier:#"segue_identifier" sender:self];
}
Now every time you want to present the viewController just tap the image
Once the destinationViewController is presented you can dismiss it with
[self dismissViewControllerAnimated:YES completion:nil];
When you right click on the viewController and the menu pops up look below Triggered Segues if manual is empty means that you haven't set a manual segue
Good Luck!

Change Background Image of an iOS app with a button click

Title pretty much says it all, I am new to Xcode and am trying to make a button that changed the view's background image. I will have three button and when you click one they will change the background that is associated with the button. How would I accomplish this? I have looked around but have not found anything relevant or anything that worked..
Thanks in advance!
EDIT::
As for what I have, I have a project set up with a window and one view with a background image called "default#2x-568h#2x.png" I would like to be able to press a button and have it switch to "second.png". I have a button and I control dragged into my .h which made this:
#interface TeslameterViewController : UIViewController <CLLocationManagerDelegate> {
IBOutlet UIButton *button1;
}
- (IBAction)button1:(id)sender;
#end
thats where I am lost.
EDIT AGAIN:
Found the fix via this video:
http://www.youtube.com/watch?feature=player_embedded&v=iKuGiJMgMiQ
On the button Action event add this line.
(void)Action:(UIButton *)sender
{
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"bg.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
try this:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]];
and also see UIView Image Background for example project with complete code...
You need to be more specific on what you need.
Within your xib file, create a UIImageView and stretch its dimensions to fit the size of the screen. Also create a the necessary buttons. Create properties in your view controller to have a reference to the UIImageView and the buttons. Give the buttons an action that changes the picture that the UIImageView loads.
You have to create three buttons in nib file and add outlet to your view controller.
To create outlet you have right click on button then you will get list of event, then control drag to your view controller header file, then one small window will pop up, write method name and click enter. Now u will see method in your controller.write code to change background of button or your view in that method.
for more details:http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphone101/Articles/05_ConfiguringView.html

ios - button not responding to click after I change its coordinates

I have a button that I configure like this in my .h file:
- (IBAction)addBusiness:(id)sender;
#property (weak, nonatomic) IBOutlet UIButton *planBusinessButton;
then I do this in my .m file:
- (IBAction)addBusiness:(id)sender
{
NSLog(#"Plan business clicked 1");
}
- (IBAction)planBusinessButton:(id)sender
{
NSLog(#"Plan business clicked 1");
}
but in my onLoad I do something like this:
CGPoint pt = planBusinessButton.center;
pt.y -= 275;
planBusinessButton.center = pt;
The problem is that the button click is not firing in the IBAction when I click the button.
Would the changing of the location of the button have an effect on why the button is not getting clicked?
Why would the button click not fire in the IBAction?
Thanks!
EDIT: Moving the button should not affect it's action firing, unless it's moved off the view or being below some other view that would intercept the tap.
If tap is not firing the button, check that:
IB connections are properly connected
methods exist in the proper location
button is enabled for user interaction
Try to avoid doing any view modifications in viewDidLoad method, as the view is not ready yet...
instead, try setting the button center in viewDidAppear method.
Possibly. Gesture Recognizers could be handling the events in the location the button moved to or there could be another view blocking the button at the new location and you need to bring the button to the front. Also, you should put this into viewDidAppear or an IBAction if you want an event to trigger it.
make sure that your planBusinessButton.frame is not outside its super.frame .
eg:if the button`s frame like this (0,-200,100,200), when the button added to its super view it will not handle the click event.

Resources