How to move an image with a button? - objective-c

I have an image(clubcow.png) that is passed from an nsarray string to make a picture. Then facedowncow represents the picture. I was wondering how to make a button that will move facedowncow to position (100,100) when button is tapped. Any tips will be appreciated. Also, there is more to this code, I just posted the important parts to give an idea on what is going on.
cardKeys = [[NSArray alloc] initWithObjects:#"clubcow", nil];
currentName = [NSMutableString stringWithFormat:#"%#.png", [cowsShuffled objectAtIndex:currentcow]];
faceDowncow = (UIImageView *)[self.view viewWithTag:1];
faceDowncow.userInteractionEnabled = YES;

I would start by creating a UIButton and adding it to your view controller's view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 20);
[button setTitle:#"Tap Me" forState:UIControlStateNormal];
[button addTarget:self action:#selector(animateImage:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Then, link this button to a function that will animate your faceDowncow object. You could add your faceDowncow as a property of the view controller so the following function can easily reference it:
- (void)animateImage:(UIButton *)sender {
[UIView animateWithDuration:0.2
animations:^{
// change origin of frame
faceDowncow.frame = CGRectMake(100, 100, faceDowncow.frame.size.width, faceDowncow.frame.size.height);
} completion:^(BOOL finished){
// do something after animation
}];
}

First of all, it looks like this code is from a view controller subclass, and the cow is a subview. In that case, you should probably have a property for it rather than obtaining it by its tag all the time. If it's instantiated in a storyboard scene/nib then you can hook up an outlet to a property/ivar in your subclass fairly easily.
The easiest way to do what you want is to create the button and use target action so that when it is tapped, it calls a method in your view controller. In the method body, obtain a reference to your cow and set it's frame property, like so:
[faceDowncow setFrame: CGRectMake(100,100,faceDowncow.bounds.size.width,faceDowncow.bounds.size.height)];
If you don't know how target action works, I suggest reading Apple's documentation on the matter. It's as simple as getting a button, calling one method to tell it what events should make a certain method get called, and then implementing that method.

Related

Common UIButton or UIbarbutton to navigationbar

How can we add common UIButton or UIBarButton or any UI object so that it appears in all navigationbars in the application.
The below code will work perfectly;
UIImage* image3 = [UIImage imageNamed:#"info.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width*4, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
//[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton setTitle:#"Category" forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(popUpPicker:)
forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
self.navigationItem.leftBarButtonItem = nil;
For these, you have to use category and then you have to call it in your every view controller to have the one code for every navigationabr button action.
There is no magic formula. One way or another, you have to add the same "UIButton or UIBarButton or any UI object" to the navigationItem of every UIViewController whose view will appear under this navigation controller's navigation bar.
You can reduce the amount of repeated code by creating this object in a single place, but the act of saying
self.navigationItem.rightBarButtonItem = thisItem
or whatever it is you want to do with the bar button item, will have to be performed separately and explicitly for every view controller.

UIButton added to subview in separate class where to store the method

In a program, I have a class called PullLeftMenu.h/.m.
In ViewController.m I am calling the PullLeftMenu like so:
PullLeftMenu *openMenu = [[PullLeftMenu alloc] init];
[openMenu classMethodHere];
So, simply runs a method with arguments. However, a part of this method of class PullLeftMenu is adding buttons to a subview that appears. Each button is assigned its own method to be called. Say for examples sake, button 1 calls method btnUsefulStuff.
I have put btnUsefulStuff method code in both the PullLeftMenu class and in the ViewController.m, and neither are being triggered - instead causing a memory crash.
Code for a button in PullLeftMenu.m
UIButton *btnUsefulStuff = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnUsefulStuff.frame = CGRectMake(10.0, 180.0, 160.0, 10.0);
[btnUsefulStuff setTitle:#"Useful Stuff" forState:UIControlStateNormal];
[btnUsefulStuff addTarget:self action:#selector(btnUsefulStuff) forControlEvents:UIControlEventTouchUpInside];
btnUsefulStuff.titleLabel.font = [UIFont fontWithName:#"KhmerUI" size:16];
btnUsefulStuff.titleLabel.textColor = [UIColor colorWithHexString:#"3a589b"];
[secondView addSubview:btnUsefulStuff];
And below this is the method:
-(void)btnUsefulStuff{
NSLog(#"button");
}
Problem is, I dont know where to place the method -(void)btnUsefulStuff as wherever I place it, it doesnt seem to get triggered and the app crashes with a memory warning. Error is:
Thread 1: EXC_BAD_ACCESS
Since this line [btnUsefulStuff addTarget:self action:#selector(btnUsefulStuff) forControlEvents:UIControlEventTouchUpInside]; is in PullLeftMenu and you're saying the target is self, that's where your method needs to be.
I suspect your problem is that you've made openMenu a local variable. Change it to a strong property inside ViewController so that it's still in memory when someone taps the button.

Navigation bar button unable to go back to the previous view

I have followed the informations that i have found online and I have managed to show the button but when i clicked on it, nothing happens. I am new to iOS programming and I am not sure if I did anything wrong.
UIImage *backImage = [UIImage imageNamed:#"backIcon.PNG"];
// create the button and assign the image to the button
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[backButton addTarget:self action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = customBarButton;
and this code for the button
- (void)goBack:(id)sender {
[self.navigationController popViewControllerAnimated:YES]; }
Thanks
First you don't have to hide the back button since you are adding your own, second try to replace your button target with the below:
[backButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
Edit:
The right way to use navigationController in storyboard will be as following:
1- Add a UInavigationController.
2- Add rootViewController relation to first VC1.
3- Add push segue from VC1 to VC2.
By this you can push and pop your VC2 properly.
Please consider to read this tutorial for better understanding storyboard, segues and navigation.

How do I make UIBarButton Work as Switch Given that UIBarButton Doesn't have Selected Property

With normal button you can do
btn.selected = !btn.selected.
How would I pull that out with UIBarButton?
You can create UIBarButtonItem with the custom view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
[button addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
Then
-(void)btnClick:(id)sender{
UIBarButtonItem *item = (UIBarButtonItem *)sender;
UIButton *button = (UIButton *)item.customView;
[button setIsSelected:YES];
}
You can use a custom Button inside UIBarItem, remain one just do with you button.
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:self.yourButton]
[self.navigationItem setRightBarButtonItem:barButton];
Then:
self.yourButton.select = !self.yourButton.select;
Idea 1. If you are creating your interface in IB, try dragging a standard UIButton object into the UIToolbar. This should encapsulate the UIButton inside a UIBarButton.
Set the UIBarButton object to be plain.
Create outlets to everything as usual.
Then you can use the standard button along with all it's lovely standard things like selected state in the toolbar.
Idea 2. I've pretty much given up on using toolbars as the controls are inflexible as you're finding. Now I just replace instances of UIToolbar with UIViews and do the small amount if code it needs to manage rotation, resizing etc... I've found it more flexible and let's me includes other controls more easily.
It is not possible to have the selected property through UIBarButtonItem straight away. But you can achieve this by placing the UIButton and customizing it like a UIBarbutton. This link will be more useful to you...How do I programmatically get the state of UIBarButtonItems?

Creating/Displaying Interface Objects with Code for the iPhone [Objective-C]

I'm currently designing an app that calls for dynamically creating objects and then displaying them in a ViewController. My problem is wading through the documentation to try and create the objects I need. I've done some searching and even tried to just sit down and figure it out, but alas, I've got nothing to show for it. I already know how to dynamically create the ViewController but I can't seem to get any objects in it.
Does anyone know how I could go about creating objects (Say a button and a slider) dynamically with Objective-C in XCode?
Creating a button in code is simple
- (void)addButton {
// Create button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Set the frame (location and size)
[button setFrame:CGRectMake(0, 0, 40, 30)];
// Set what happens when you touch the button
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
// Set button text
[button.titleLabel setText:#"Button"];
// Add button to view
[self.view addSubview:button];
}
A lot of interface elements follow a similar patter- alloc and init the object, set the frame and add it to a view.