IBAction cycle through images on tap - ios

I want to use an IBAction method that when on tap cycles through images. Ie one image and a button is shown unload and when the user taps the button a new image is shown in its place etc. Right now it just shows the first image in the array. When I tap the button a second time nothing happens. Can someone please help me edit this to cycle through all of my images? Thanks.
EDIT
//when i tap the button only the first image shows in the array. What should I edit from the current app?
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Implementing our method
- (IBAction)buttonPressed
{
NSArray *imagesArray = #[#"image1.png",#"image2.png",#"image3.png",#"image4.png"];
count ++;
if(count == imagesArray.count)
{
count = 0;
}
yourImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:count]];
}
#end

try this....
add images into your project and store those images names into an NSArray
for ex.
//declare imagesArray as instance variable
NSArray *imagesArray = #[#"image1.png",#"image2.png",#"image3.png",#"image4.png"];
and in the button action method
- (IBAction)buttonPressed
{
count ++;
if(count == imagesArray.count)
{
count = 0;
}
yourImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:count]];
}

Related

Instantiating UIActivity subclass

When sharing an image from, eg Photos, I want my iOS app extension to appear in the list of apps available (next to Mail, Messages...) in order to receive the shared picture. Reading many sites reveals I have to subclass UIActivity and add it to an App Extension, what I did.
However I don't understand how I should instantiate my subclass, and it never gets called (No NSLog is displayed, item not in the sharing list).
How should I "instruct" iOS to use this subclass ?
myActivity.m:
#import "myActivity.h"
#implementation myActivity
- (NSString *)activityType {
// a unique identifier
return #"com.myapp.uniqueIdentifier";
}
- (NSString *)activityTitle {
// a title shown in the sharing menu
return #"Custom Activity";
}
- (UIImage *)activityImage {
// an image to go with our option
return [UIImage imageNamed:#"MyImage"];
}
+ (UIActivityCategory)activityCategory {
// which row our activity is shown in
// top row is sharing, bottom row is action
NSLog(#"UIActivityCategoryShare");
return UIActivityCategoryShare;
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
// return YES for anything that our activity can deal with
for (id item in activityItems) {
// we can deal with strings and images
if ([item isKindOfClass:[UIImage class]]) {
return YES;
}
}
// for everything else, return NO
return NO;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems {
// anything we need to prepare, now's the chance
// custom UI, long running calculations, etc
// also: grab a reference to the objects our user wants to share/action
self.activityItems = activityItems;
}
- (UIViewController *)activityViewController {
// return a custom UI if we need it,
// or the standard activity view controller if we don't
return nil;
}
- (void)performActivity {
// the main thing our activity does
// act upon each item here
for (id item in self.activityItems) {
NSLog(#"YEY - someone wants to use our activity!");
NSLog(#"They used this object: %#", [item description]);
}
// notify iOS that we're done here
// return YES if we were successful, or NO if we were not
[self activityDidFinish:YES];
}
#end
And myActivity.h:
#import <UIKit/UIKit.h>
#interface myActivity : UIActivity
#property (nonatomic, strong) NSArray *activityItems;
#end

How do I display an alternate message if an object does not exist in my array? Objective C

The object of this application is to translate between english and spanish words.
(Checks text input against all array values to see if it's there and then compares that index to the second array, and displays the parallel value).
That part is working. If the word entered does not exist in the array, I am supposed to have a message like 'No translation available' display in the label. My problem is, I can either get the message to display for nothing or everything - rather than just when it is supposed to.
#import "TranslatorViewController.h"
#interface TranslatorViewController ()
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)translate:(id)sender;
#property (nonatomic, copy) NSArray *english;
#property (nonatomic, copy) NSArray *spanish;
#end
#implementation TranslatorViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textField.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//make the keyboard go away
-(BOOL) textFieldShouldReturn:(UITextField *)textField {
{[textField resignFirstResponder];}
return YES;
}
- (instancetype)initWithCoder:(NSCoder*)aDecoder {  self = [super initWithCoder:aDecoder]; if(self) {  // Add your custom init code here
self.english = #[#"phone",
#"dog",
#"sad",
#"happy",
#"crocodile"];
self.spanish = #[#"telefono",
#"perro",
#"triste",
#"felize",
#"cocodrilo"];
}  return self; }
- (IBAction)translate:(id)sender {
//loop through the array
NSString *englishWord = self.textField.text;
for (int index=0; index<[self.english count]; index++)
if([[self.english objectAtIndex:index]
isEqualToString:englishWord])
//retrieve the accompanying spanish word if english word exists in the array
{NSString *spanishWord = [self.spanish objectAtIndex:index];
//and display it in the label
self.label.text = spanishWord;}
//Need code to display 'no translation' if word was not found.
}
#end
The simplest way to do this is probably to set the label's text field to "No translation" before entering the loop. If no match is found, the label will never be re-set to anything else.
There are lots of other ways to structure logic to give you this result. I might tighten up that last loop of code by doing this instead:
NSString * englishWord = self.textField.text;
NSUInteger spanishWordIndex = [self.english indexOfObject:englishWord];
if (spanishWordIndex == NSNotFound) {
self.label.text = #"No translation";
} else {
self.label.text = self.spanish[spanishWordIndex];
}
Why not use an NSDictionary?
- (instancetype)initWithCoder:(NSCoder*)aDecoder {
self = [super initWithCoder:aDecoder];
if(self) {  // Add your custom init code here
self.translationDict = #{#"phone":#"telefono",
#"dog":#"perro",
#"sad": #"triste",
#"happy": #"felize",
#"crocodile": #"cocodrilo"]; // self.translationDict is an NSDictionary
}
return self;
}
- (IBAction)translate:(id)sender {
NSString *englishWord = self.textField.text;
NSString *spanishWord=self.translationDict[englishWord];
if (spanishWord == nil) {
spanishWord="No Translation";
}
self.label.text=spanishWord;
}
I put
self.label.text = #"No translation available";
before the if statement which is I believe what Ben Zotto was trying to say! I just wasn't sure how to actually do it at first.
I'm a newb with this stuff.
But that did what I needed it to.
Thanks all!
Reformatted the answer to include your entire code. You should be able to just copy paste into your code.
- (IBAction)translate:(id)sender {
NSString *englishWord = self.textField.text;
NSString *spanishWord = #"No translation found.";
for (int index=0; index<[self.english count]; index++)
{
if([[self.english objectAtIndex:index] isEqualToString:englishWord])
{
//retrieve the accompanying spanish word if english word exists in the array
spanishWord = [self.spanish objectAtIndex:index];
}
}
self.label.text = spanishWord;
}

Display photos in different UIImageViews

In my test app, I have two buttons (takePhoto1 and takePhoto2) and two UIImageViews (photo1View and photo2View). The first button takes a photo and displays it in the first image view. The second button takes another photo, and is supposed to display it in the second image view. However, for some reason second photo is displayed in photoView1 instead of photoView2.
I haven't implemented any code to save the photos yet, so I'm not worried about the photos persisting in the image views.
What I do need to figure out is how to make sure when the second photo is taken, it is displayed in the appropriate view. Here is what I have so far:
//Take photo1 and display in top image view
- (void)takePhoto1;
{
[self dismissViewControllerAnimated:YES completion:NULL];
if ([self.capturedImages count] > 0)
{
if ([self.capturedImages count] == 1)
{
// Camera took a single picture.
[self.photo1View setImage:[self.capturedImages objectAtIndex:0]];
}
// To be ready to start again, clear the captured images array.
[self.capturedImages removeAllObjects];
}
self.imagePickerController = nil;
}
//Take photo2 and display in bottom image view
- (void)takePhoto2;
{
[self dismissViewControllerAnimated:YES completion:NULL];
if ([self.capturedImages count] > 0)
{
if ([self.capturedImages count] == 1)
{
// Camera took a single picture.
[self.photo2View setImage:[self.capturedImages objectAtIndex:1]];
}
// To be ready to start again, clear the captured images array.
[self.capturedImages removeAllObjects];
}
self.imagePickerController = nil;
}
Anyone know how to fix this?
I saw your questions an couldn't help my self to solve it in my way. If I had same problem as yours I would have solved it in this way:
Consider, button's tag is same as, imageView tag.
#import "ViewController.h"
#interface ViewController ()
- (IBAction)takePhoto:(id)sender;
#property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *imageViews;
#property int tag;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)takePhoto:(id)sender {
_tag = ((UIButton *)sender).tag;
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
#pragma mark - UIImagePickerDelegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
[picker dismissViewControllerAnimated:NO completion:nil];
[[self getImageView:_tag] setImage:image];
}
- (UIImageView *) getImageView : (int) tag{
for (UIImageView *imageView in _imageViews) {
if (imageView.tag == tag) {
return imageView;
}
}
return nil;
}
#end
Here you can check this as working project here.
Check your storyboard or xib to see if "photo2View" is really connected to your outlet.
I suspect the UIImageView for "photo1View" is also connected to "photo2View".
p.s. you should also change this line in your #implementation:
- (void)takePhoto2;
to this:
- (void)takePhoto2
semi-colons should only come after method declarations in your .h file.
Does your code work? I find two issues -
Issue 1 - This should crash as capturedImages has one element according to second if check but you are accessing second element in the array while setting the imageView.
if ([self.capturedImages count] == 1)
{
// Camera took a single picture.
[self.photo2View setImage:[self.capturedImages objectAtIndex:1]];
Issue 2 - Since you are clearing the self.capturedImages, this should have thrown you can exception in second method.
[self.photo2View setImage:[self.capturedImages objectAtIndex:1]];
Did you properly connect button 1 to takePhoto1 and button 2 to takePhoto2? Maybe button 2 is also doing takePhoto1?

Check Which Order Buttons Are Pressed Objective-C

I am developing an app that I need to check which order buttons are pressed in. I had 3 buttons and if they are pressed in the incorrect order I will have a UIAlertView. How can I check the order of presses?
Thanks
You could wire up an action to the buttons (like "Touch Up Inside"), and have it log which buttons are pressed, and maybe have a counter incrementing as well. Then when the counter gets to three, have it go through the list of button presses, and verify if they are the order you anticipate.
Below is an example of what I mean. For this example, you have to wire up all 3 buttons "Touch Up Inside" to that same IBAction. Of course you replace the NSLogs with your UIAlertView, but this shows the gist of what I said.
#interface comboSOTestViewController ()
#property (strong, nonatomic) NSMutableArray *buttonTitles;
#end
#implementation comboSOTestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.buttonTitles = [[NSMutableArray alloc]init];
}
- (IBAction)comboButtonPress:(UIButton *)sender
{
[self.buttonTitles addObject:sender.titleLabel.text];
if (self.buttonTitles.count > 2)
{
BOOL bad = NO;
NSArray *correctOrder = #[#"Second", #"Third", #"First"];
for (int i=0; i < 3; i++)
{
if (![self.buttonTitles[i] isEqualToString:correctOrder[i]])
{
bad = YES;
}
}
if (bad == YES)
{
NSLog(#"WRONG ORDER");
}
else
{
NSLog(#"CORRECT ORDER");
}
}
}

If Picture A is displayed then Text A must be entered else LABEL say Wrong

I'm sorry if this seems stupid to you but I need help creating something like this:
If Picture A is displayed in an UIImageView the UITextfield has to be PictureA-description.
So let's say a picture of the USA flag is displayed. The user needs to enter USA to get to next view otherwise the UILabel will say "Wrong".
When I tried with:
#implementation ViewController
-(IBAction)ButtonTouched:(id)sender
{
[self.inputfield resignFirstResponder];
if (_inputfield.text isEqualtoString #"USA") //BUT IT SAYS PARSE ISSUE EXPECTED')'
{
__errorlabel.text = #"Correct";
sleep(1);
[self performSegueWithIdentifier:#"Correct1" sender:self];
}
else
{
__errorlabel.text = #"Wrong Answer";
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
What can I do?
The line
if (_inputfield.text isEqualtoString #"USA")
is completely wrong spelled/typed. It must be
if ([_inputfield.text isEqualToString:#"USA"])
or better
if ([self.inputfield.text isEqualToString:#"USA"])
and as suggested by Jeremy a case insensitive example:
if ([[self.inputfield.text uppercaseString] isEqualToString:#"USA"])
or
if (NSOrderedSame == [self.inputfield.text caseInsensitiveCompare:#"USA"])
You should definitely look into objective-c basics! A good starting point is here. More on NSString can be learned here.

Resources