How to randomize 3 images in a ViewController? - ios

In my app I have a main viewcontroller with an imageview. Every time the user goes back to this main view, I would like 1 of 3 images to appear, but I would like to randomize them. Does anyone know the best way to get this done? Could someone point me in the right direction?
It's a universal ios app in xcode with storyboard.
Thanks!

Something like the following should do the trick...
NSArray *imageArray = #[#"ImageName1", #"ImageName2", #"ImageName3"];
NSUInteger randomImageIndex = arc4random() % [imageArray count];
[yourImage setImage:[UIImage imageNamed:[imageArray objectAtIndex:randomImageIndex]]];
Edit:
Add the following to your view controllers .h file.
#property (strong, nonatomic) IBOutlet UIImageView *yourImage;
Then within your storyboard/view link the UIImageView to yourImage.

Related

how to get image name when click on uiimageview?

I have a UIScrollview.
In the UIScrollview there are 4 UIImageViews with images.
When I click on any UIImageview I want to know the name of that image.
UIImageView just keeps the pointer to the place the UIImage is stored inside the memory, so it's not possible with UIImageView. You would need to create a subclass of UIImageView with a NSString variable which stores the value to access it later. Something like this:
#import <UIKit/UIKit.h>
#interface MyImageView : UIImageView
#property (strong, nonatomic) NSString *imageName;
#end
The image names are not stored in UIImage objects so you have to keep them somewhere else for lookup. To detect touches in image views you either have to subclass them and add code for touch detection, add tap recognizers or replace them with buttons.

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;
}

How do I make an image cover the screen when a view is loading?

I have a webview and when you click a link on a webpage, I want the screen to show an image for the duration of the time that the page is laoding (it sounds weird, but it's for a good reason).
I'm new to XCode and have done a few tutorials, but it's still murky to me how to make things happen in XCode (and objective C in general).
It seems that the process should be this - in the webViewDidStartLoad method, I call a class, which is a UIImageView. In that class is the image. And in webViewDidFinishLoad, I then either remove the image, or lower it on the stack.
I just am confused as to all the things you need to do to make this happen. Here's what I have so far:
A class called loadingView which has a method in it called showLoadingScreen. In this method I have the code:
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"Icon-76.png"]]];
Then I go to the storyboard and insert a new UIView over the webView, and give it the class loadingView. And then I control clicked from this UIView to the ViewController.h, and dropped it in the view controller, creating the code:
#property (strong, nonatomic) IBOutlet UILabel *loadingPageView;
and imported loadingView.h.
Is this correct? If yes, then I need to put this method into webViewDidStartLoad, but I can't figure out what code to put there. It seems I to do something like:
[self.view addSubview:imageView];
But I don't know how to customize this to my code.
If you want a view to cover the screen, use:
[[[UIApplication sharedApplication] keyWindow] addSubview:myImageView];
This will add your UIImageView to your key UIWindow, which will show over any UIViewController. Otherwise, add your UIImageView as a subview of your UIWebView. If you want to guarantee that your UIImageView is at the top of the view stack, call:
[self.webView bringSubviewToFront:myImageView];
Either way, maintain your UIImageView as a global property/ivar so you can later call:
[myImageView removeFromSuperview];
Step 1: Declare an imageview property somewhere:
#property (strong, nonatomic) UIImageView * coverImageView;
Step 2: Show Image view in webViewDidStartLoad
- (void) webViewDidStartLoad:(UIWebView *)webView {
if (!_coverImageView) _coverImageView = [[UIImageView alloc]init];
_coverImageView.frame = self.view.bounds;
_coverImageView.image = [UIImage imageNamed:#"Icon-76.png"];
[self.view addSubview:_coverImageView];
}
Step 3: Remove Image View in webViewDidFinishLoad
- (void) webViewDidFinishLoad:(UIWebView *)webView {
[_coverImageView removeFromSuperview];
}

I have 15 UIButton in my iPhone project, after clicking on button one image is pop up and all images are different for different button?

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;
}
}
}

Change UIImageView Image path

I'm completely new to objective-c and iOS programming with it. I am displaying a UIImageView with a default image path I set via storyboard. Now, I am trying to change the image to a different image, by changing the path to a different one.
I think I have set everything up so that I can programmatically manipulate the class (IBOutlet, #property, allocing, etc), but perhaps I am doing something wrong there. Otherwise, I am changing the image path via:
IMG.image = [UIImage imageNamed:#"s.png"];
Is this correct? If so, here is the other piece of relevant code in my .m file:
- (void)viewDidLoad
{
_IMG = [[UIImageView alloc] init];
}
In the #interface part of my .h file:
IBOutlet UIImageView *IMG;
And then the rest of my .h file:
#property (strong, nonatomic) IBOutlet UIImageView *IMG;
Does anyone see anything incorrect? Thanks, I am a complete beginner so I apologize if this is an extremely basic thing.
Firstly, get rid of this line from your .h (you don't need it as you are declaring a property):
IBOutlet UIImageView *IMG;
Next, check that you have wired up your IBOutlet property to the UIImageView in Interface Builder (hold down CTRL and click on your UIImageView). Make sure there is an entry under Referencing Outlets. If not you will need to CTRL-drag to create one.
Hope this helps.
If you are using storyboard you dont need to alloc init your view, remove that code.

Resources