iOS: How to change UIImage with UIButton [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This might be simple, but I'm very new at developing an iOS APP. I have an UIButton and three UIImage's. What I'm trying to do is that when the user taps the button, the first image will display, if the user taps it again, then show the second image, and so on. When the last image is displaying, then go back to the first one when tapping the button again. The problem is that only the first image appears.
Here is my code:
file.h
#interface ViewController : UIViewController
{
IBOutlet UIImageView *images;
}
-(IBAction)changeImage:(id)sender;
#end
file.m
#implementation ViewController
-(IBAction)changeImage:(id)sender{
images.image = [UIImage imageNamed:#"pic1.png"], [UIImage imageNamed:#"pic2.png"],[UIImage imageNamed:#"pic3.png"];
}
Any help would be greatly appreciated!
Than you in advance!

You can declare a global variable for the index and global variable array to hold the image names
self.index = 0;
self.imageList = [NSArray arrayWithObjects:#"pic1", #"pic2", #"pic3", nil];
then in the click action, you can do
-(IBAction)changeImage:(id)sender{
images.image = [UImage imageNamed:self.imageList[self.index]];
self.index = (self.index + 1 ) % [self.imageList count];
}
I don't have the compiler with me to verify the syntax, but you get the idea.

This is easy, create a counter called tapCounter in viewDidLoad. Let's imagine you already set your image array as follows in viewDidLoad. Make sure images and tapCounter are properties of your class
- (void)viewDidLoad {
[super viewDidLoad];
self.images = #[[UIImage imageNamed:#"pic1.png"], [UIImage imageNamed:#"pic2.png"],[UIImage imageNamed:#"pic3.png"]];
self.tapCounter = 0;
}
-(IBAction)changeImage:(id)sender {
UIButton *but = (UIButton *)sender;
tapCounter++;
if (tapCounter == 4) {
tapCounter = 1;
}
[but setBackgroundImage:[images objectAtIndex:tapCounter-1] forState:UIControlStateNormal];
}
}

Related

How to create many similar ViewControllers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to make a simple ViewController with two or three Buttons and labels. When I press the Button it plays an audio file.
I want to make hundreds of this similar screen, what is the best way to create it? (I am currently creating it with MainStoryBoard.)
For each page I would like to make small changes such as Buttons sizes ,Buttons numbers, label texts and audio files.
Drawing text bubbles or using xib file might be good, but I am not sure what I should.
You first need to have your own class for this UIViewController as a base class that has buttons, labels, etc.
Then use Factory Design pattern to generate the inherited UIViewController class which allows you to do some tweak that fits you need.
Factory Design Pattern
BaseViewController {
UIButton *button1;
UIButton *button1;
UILabel *label1;
}
ChildViewControllerA : BaseViewController {
UIButton *button3
}
ChildViewControllerB : BaseViewController {
UIButton *button4
}
Factory : NSObject {
+ (BaseViewController)generateChildViewController: (int) type {
switch (type)
case 0:
return [[ChildViewControllerA alloc] init];
case 1:
return [[ChildViewControllerB alloc] init];
}
}
Main {
- (void)createThreeViewControllers {
BaseViewController *vc1 = [Factory generateChildViewController:0];
vc1.button1.backgroundColor = [UIColor blueColor];
BaseViewController *vc2 = [Factory generateChildViewController:0];
vc2.button2.center = cgpointmake (100, 150);
BaseViewController *vc3 = [Factory generateChildViewController:0];
vc3.label1.text = #"vc3 test";
}
}

Display images dynamicalli in objective -c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want images to be displayed dynamically, for ex- if user selects 10 images, all those should get displayed, now if user wants to select 5 more images, so (10+5) 15 images should get displayed. How to achieve this functionality?
I am using UIImagePickerController to select images, so at a time user will be able to select 1 image only.I have given a add photo button.
UIImagePickerController *galleryObj=[[UIImagePickerController alloc]init];
galleryObj.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
galleryObj.allowsEditing = YES;
galleryObj.delegate=self;
[self presentViewController:galleryObj animated:YES completion:nil];
Above will get executed when user taps on add photo button.
//GalleryViewcontroller.h
#import <UIKit/UIKit.h>
#interface GallaryViewController : UIViewController<UIImagePickerControllerDelegate>
{
NSMutableArray *selectedImagesArray;
}
#end
GalleryViewcontroller.m
#implementation GallaryViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//allocating array
selectedImagesArray=[[NSMutableArray alloc]init];
}
-(IBAction)openGallery:(id)sender
{
UIImagePickerController *galleryObj=[[UIImagePickerController alloc]init];
galleryObj.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
galleryObj.allowsEditing = YES;
galleryObj.delegate=self;
[self presentViewController:galleryObj animated:YES completion:nil];
}
#pragma mark UIImagePickerController Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
//here we are adding the image in array
[selectedImagesArray addObject:[info objectForKey:UIImagePickerControllerOriginalImage]];
NSLog(#"%#",selectedImagesArray);
//reload your colleciton view or tableview or any other view which your using to show selected images
//you can get images by index
//UIImage *img=[selectedImagesArray objectAtIndex:0];
}];
}
#end

How to create a tip balloon? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here is my image.I want to create a tip balloon like this. This must visible when user types in textfield
I have created one more but that is in cloud shape. Please help to create this
Subclass the UITextField then override becomeFirstResponder (show bubble) and resignFirstResponder (hide bubble) and when the field is active present your bubble.
.h
#import <UIKit/UIKit.h>
#interface BubbleTextField : UITextField
#end
.m
#import "BubbleTextField.h"
#interface BubbleTextField ()
{
UIView *_bubbleView;
}
- (void)showBubble:(BOOL)show;
#end
#implementation BubbleTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialize the _bubbleView here
}
return self;
}
#pragma mark - Override
- (BOOL)becomeFirstResponder {
BOOL shouldBecome = [super becomeFirstResponder];
if (shouldBecome)
{
[self showBubble:YES];
}
return shouldBecome;
}
- (BOOL)resignFirstResponder {
BOOL shouldResign = [super resignFirstResponder];
if (shouldResign)
{
[self showBubble:NO];
}
return shouldResign;
}
#pragma mark - Private Methods
- (void)showBubble:(BOOL)show {
// Show/Hide
// Animations etc.
}
#end
Check out KBPopupBubble.
And CMPopTipView
Also, check out these search results for custom controls on cocoa controls. Search Results.

Detect tap on CCTableView cell

I am using a CCTableView to make a table with CCNodes as cells. Those CCNodes have a button each. I want to be able to detect if a user taps on a cell and if it taps on the button. But the CCTableView doesn't have a tableView:didSelectRowAtIndexPath: method so how can I do this? Do you know of any open source class that has this method?
P.S. I am using version 3 of cocos2d
I took a different approach after trying numerous things
#interface WKTableCell : CCTableViewCell
#end
#implementation WKTableCell
- (instancetype) initWithTitle: (NSString *) title
{
self = [super init];
if (!self)
return nil;
[self.button setTitle:title];
// This is a transparent png (400x200) for my needs
CCSpriteFrame * frame = [CCSpriteFrame frameWithImageNamed:#"cell.png"] ;
[self.button setPreferredSize:CGSizeMake(frame.originalSize.width, frame.originalSize.height)];
[self.button setContentSizeType:CCSizeTypePoints];
[self.button setBackgroundSpriteFrame:frame forState:CCControlStateNormal];
}
// then in your table
[table setBlock:^(id sender) {
CCLOG(#"yup, this gets called.. ");
}];
this did work for me..
Your CCTableView, responds to the CCTouchDelegate, so you can use ccTouchBegan etc
to detect the point and then calculate what cell there was in that point. Here the class reference:
http://docs.huihoo.com/doxygen/cocos2d-x/2.1.2/d0/d38/classcocos2d_1_1_c_c_touch_delegate.html

Change button title [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i created nine buttons using IBOutletCollection here.
My question is - i want to get title which button is pressed and display it on another buttons title. Is this possible!
Try like this,
- (IBAction)buttonAction:(UIButton *)btn {
yourNextVCobject.buttonTitle = [NSString stringWithFormat:#"%#",btn.titleLabel.text];
// then push to ur vc.
}
In your Next VC,
In .h
#property (nonatomic, retain) NSString *buttonTitle;
In .m
[yourAnotherBtn setTitle:self.buttonTitle forState:UIControlStateNormal];
You can use the below code to set the title of clicked button to targetted another button.
-(IBAction)clickbutton:(id)sender
{
UIButton *firstButton = (UIButton *)sender;
NSString *neededtitle = [NSString stringwithformat:#"%#",firstButton.currentTitle];
[anotherbutton setTitle:neededtitle forState:UIControlStateNormal];
}

Resources