Trying to create an image picker with a tap gesture, so when the default picture is tapped it allows the user to pick an image from the device's photo library. For some reason when I click the view in the iOS simulator it simply doesn't do anything and I can't figure out why.
This is what I have so far. I'm pretty new to Objective-C
ViewController.m :
#import "ViewController.h"
interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UITextField *brandField;
#property (weak, nonatomic) IBOutlet UITextField *priceField;
#property (weak, nonatomic) IBOutlet UITextField *additionalNotesField;
#property (weak, nonatomic) IBOutlet UIButton *addItem;
#property (weak, nonatomic) IBOutlet UIButton *cancelAdditem;
#property (weak, nonatomic) IBOutlet UIImageView *toyImage;
- (IBAction)imageSelect:(UITapGestureRecognizer *)sender;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_nameField.delegate = self;
_brandField.delegate = self;
_priceField.delegate = self;
_additionalNotesField.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
-(BOOL) textFieldShouldReturn:(UITextField *) textField {
textField.resignFirstResponder;
return true;
}
-(void) textFieldDidEndEditing:( UITextField *) textField {
_nameField.text = textField.text;
_brandField.text = textField.text;
_priceField.text = textField.text;
_additionalNotesField.text = textField.text;
}
- (IBAction)imageSelect:(UITapGestureRecognizer *)sender {
// Hide the keyboard
_nameField.resignFirstResponder;
_brandField.resignFirstResponder;
_priceField.resignFirstResponder;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init]; // Creates image picker
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // Source for picker is saved photos. Only allow photos to be picked, not taken
// Make sure the picker is notified when the user picks an image
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:true completion:nil];
}
// UIImagePickerControllerDelegete
-(void) imagePickerDidCancel:(UIImagePickerController *) imagePicker {
[self dismissViewControllerAnimated:true completion:nil]; // Dimiss the picker if the user canceled
}
-(void) imagePicker:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* selectedImage = info[UIImagePickerControllerOriginalImage];
_toyImage.image = selectedImage; // Set the image view to display the selected image
// Dimiss the picker
[self dismissViewControllerAnimated:true completion:nil];
}
#end
As Larme said, I needed to add this line:
_toyImage.userInteractionEnabled = YES;
Related
My application has a Facebook Login screen, which after login shows a profile picture, username, email, and logout button.
I am trying to figure out how to link the current users profile picture with a UIView or UIImageView (which ever is more applicable, given I used a UIView called FBProfilePictureView) in the main VC called HomeViewController. Here is the code I have for the LoginViewController that has the FB information asked for and returned. I also have it segue to the HomeViewController once the user info has been entered.
LoginViewController.h:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <FacebookSDK/FacebookSDK.h>
#interface LoginViewController : UIViewController <FBLoginViewDelegate>
#property (weak, nonatomic) IBOutlet FBLoginView *loginButton;
#property (weak, nonatomic) IBOutlet UILabel *lblLoginStatus;
#property (weak, nonatomic) IBOutlet UILabel *lblUsername;
#property (weak, nonatomic) IBOutlet UILabel *lblEmail;
#property (weak, nonatomic) IBOutlet FBProfilePictureView *profilePicture;
#property (strong, nonatomic) IBOutlet UIImageView *loginwallpaper;
#property (strong, nonatomic) IBOutlet UIImageView *loggedinwallpaper;
#property (strong, nonatomic) IBOutlet UIImageView *FBlogin;
#property (strong, nonatomic) IBOutlet UIImageView *FBlogout;
#end
and here is the LoginViewController.m
#import "LoginViewController.h"
#interface LoginViewController ()
- (void)toggleHiddenState:(BOOL)shouldHide;
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self toggleHiddenState:YES];
self.lblLoginStatus.text = #"";
self.loginButton.readPermissions = #[#"public_profile", #"email"];
self.loginButton.layer.cornerRadius = 0;
[self.loginButton.layer setBorderWidth:0.0f];
self.loginButton.delegate = self;
// Do any additional setup after loading the view.
}
-(void)toggleHiddenState:(BOOL)shouldHide{
self.lblUsername.hidden = shouldHide;
self.lblEmail.hidden = shouldHide;
self.profilePicture.hidden = shouldHide;
self.loggedinwallpaper.hidden = shouldHide;
self.FBlogout.hidden = shouldHide;
}
-(void)toggleUnhiddenState:(BOOL)shouldShow{
self.loggedinwallpaper.hidden = NO;
}
-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{
self.lblLoginStatus.text = #"";
[self toggleHiddenState:NO];
[self toggleUnhiddenState:YES];
}
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
self.lblLoginStatus.text = #"";
[self toggleHiddenState:YES];
}
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
NSLog(#"%#", user);
self.profilePicture.profileID = user.objectID;
self.lblUsername.text = user.name;
self.lblEmail.text = [user objectForKey:#"email"];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabcontroller = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self presentViewController:tabcontroller animated:YES completion:nil];
}
-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error{
NSLog(#"%#", [error localizedDescription]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
All of the other questions posted on here have not really helped me - they have been too vague.
If someone could just show me how to connect this profile picture to a UIImageView or UIView in the HomeViewController that would be awesome! I know this is easy and I'm just frustrated that this is giving me such an problem
(I know that I need to use the prepareForSeuge method I am just not sure on the syntax of how to accomplish this.)
It will help you. Go to HomeViewController.xib and here put UIView by dragging it.
Now, select that view and on the right side of XCode click on third tab that is Identity Inspector. Here set the class name as FBProfilePictureView then try.
If you want to diplay image in FBProfilePictureView then
Xib/StoryBoard -> Identity Inspector -> class name -> FBProfilePictureView
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
NSLog(#"%#", user);
self.profilePicture.profileID = user.id;
}
If you want to display in imageview
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
dispatch_async( queue, ^{
// Load UImage from URL
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://graph.facebook.com/%#/picture?width=200&height=200", user.id]];
NSData *data = [NSData dataWithContentsOfURL:url];
// Then to set the image it must be done on the main thread
dispatch_sync( dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:data];
yourImageView.image = image
image = nil;
});
});
Im following this tutorial:
http://www.raywenderlich.com/1845/ios-tutorial-how-to-create-a-simple-iphone-app-tutorial-part-2
I get the following errors:
1.Property implementation must have its declaration in interface "RWTDetailViewController"
2.Property "data" not found on object of type "id"
Here is my code:
#import "RWTScaryBugDoc.h"
#import "RWTScaryBugData.h"
#import "RWTUIImageExtras.h"
#import "RWTDetailViewController.h"
#interface RWTDetailViewController ()
- (void)configureView;
#end
#implementation RWTDetailViewController
#pragma mark - Managing the detail item
#synthesize picker = _picker;
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
self.rateView.notSelectedImage = [UIImage imageNamed:#"shockedface2_empty.png"];
self.rateView.halfSelectedImage = [UIImage imageNamed:#"shockedface2_half.png"];
self.rateView.fullSelectedImage = [UIImage imageNamed:#"shockedface2_full.png"];
self.rateView.editable = YES;
self.rateView.maxRating = 5;
self.rateView.delegate = self;
if (self.detailItem) {
self.titleField.text = self.detailItem.data.title;
self.rateView.rating = self.detailItem.data.rating;
self.imageView.image = self.detailItem.fullImage;
}
}
- (IBAction)titleFieldTextChanged:(id)sender
{
self.detailItem.data.title = self.titleField.text;
}
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#pragma mark RWTRateViewDelegate
- (void)rateView:(RWTRateView *)rateView ratingDidChange:(float)rating
{
self.detailItem.data.rating = rating;
}
- (IBAction)addPictureTapped:(id)sender {
if (self.picker == nil) {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.picker.allowsEditing = NO;
}
[self presentViewController:_picker animated:YES completion:nil];
}
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *fullImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *thumbImage = [fullImage imageByScalingAndCroppingForSize:CGSizeMake(44, 44)];
self.detailItem.fullImage = fullImage;
self.detailItem.thumbImage = thumbImage;
self.imageView.image = fullImage;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation
{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
When I move my #synthesize into the interface-declaration, I the the following error: "Illegal interface qualifier"
Can anyone help me?
Here is my .h-file:
#import <UIKit/UIKit.h>
#import "RWTRateView.h"
#interface RWTDetailViewController : UIViewController <UITextFieldDelegate, RWTRateViewDelegate, UIImagePickerControllerDelegate, UINavigationBarDelegate>
#property (strong, nonatomic) id detailItem;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet RWTRateView *rateView;
#property (weak, nonatomic) IBOutlet UITextField *titleField;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (strong, nonatomic) UIImagePickerController *picker;
- (IBAction)addPictureTapped:(id)sender;
- (IBAction)titleFieldTextChanged:(id)sender;
#end
Error description is self explanatory, you need to declare picker as a property in interface(.h) file before you move to implementation(.m) file and synthesize it. However you don't need to synthesize properties anymore but if you do that is also fine.
One more thing, you missed the part from tutorial in which writer has declared this property in .h file already. You can have a look again or add this line to RWTDetailViewController.h
#property (strong, nonatomic) UIImagePickerController *picker;
EDIT
For second error you are declaring detailItem as an object of type id, however writer is using a custom class RWTScaryBugDoc in which he should be having a property named data. That's why you are getting the error as property data doesn't belong to id type. You may want to download the sample code at the end of tutorial and cross check the missing points.
You just need to declare your property first. So in the interface (h file or top of your m file), add:
#property (nonatomic, strong)UIImagePickerController *picker;
The #synthetise need to stay in the implementation.
You declare self.detailItem as Id. Id is no class and so it has no attribute, but you try to access self.detailItem.data.... This is not possible.
I can't help you to fix this, because there are so many logical erros in your code, that 99% of it makes no sense for me.
I'd appreciate if anyone could help.
I've created a slider and a label that changes it's text value when the user changes the slider. It's not working, for some reason.
Thoughts ?
I'm using xcode 6.1
Here's the code:
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *numberField;
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sliderLabel.text = #"50";
}
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)backgroundTap:(id)sender {
[self.nameField resignFirstResponder];
[self.numberField resignFirstResponder];
}
- (IBAction)sliderChanged:(UISlider *)sender {
int progress = lroundf(sender.value);
self.sliderLabel.text = [NSString stringWithFormat:#"%d", progress];
}
#end
take UISlider outlet and add selector as value changed in viewDidLoad like below.
IBOutlet UISlider *slider;
and to add selector in viewDidLoad
[slider addTarget:self action:#selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
When I try to change the text of a label on button click, the code compiles but it doesn't work. I click the button and nothing happens.
Currently, im just trying to display "Error" on label x1Label when button solveButton will be pressed. Could you please help me fixing it?
.h
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize errorLabel;
#synthesize aField, bField, cField;
#synthesize x1Label, x2Label;
#synthesize solveButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aField.delegate = self;
bField.delegate = self;
cField.delegate = self;
aField.keyboardType = UIKeyboardTypeNumberPad;
bField.keyboardType = UIKeyboardTypeNumberPad;
cField.keyboardType = UIKeyboardTypeNumberPad;
NSString *str = #"car";
errorLabel.text = str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
}
- (IBAction)solveButton:(id)sender
{
errorLabel.text = #"Error";
}
#end
.m
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *aField, *bField, *cField;
}
#property (weak, nonatomic) IBOutlet UILabel *errorLabel;
#property (strong, nonatomic) IBOutlet UITextField *aField;
#property (strong, nonatomic) IBOutlet UITextField *bField;
#property (strong, nonatomic) IBOutlet UITextField *cField;
#property (weak, nonatomic) IBOutlet UILabel *x1Label;
#property (weak, nonatomic) IBOutlet UILabel *x2Label;
#property (weak, nonatomic) IBOutlet UIButton *solveButton;
#end
Thank you
You are writing on errorLabel not on x1Label.
Did you connect the SolveButton action to the button in your storyboard?
if not go to storyboard > click on your button > go to Connection Inspector (on the right panel) and link the action to you button.
I followed this tutorial on how to add custom info window to a google map marker,
in the UIView I've added a button and created an IBAction but when I click on it nothing happen
my infoWindow view code looks like this
.h
#import <UIKit/UIKit.h>
#import "Details.h"
#interface MarkerInfoWindowView : UIView
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#property (weak, nonatomic) IBOutlet UIButton *btn1;
- (void) initializeWithDetails:(Details*)p_details;
#end
.m
#import "MarkerInfoWindowView.h"
#implementation MarkerInfoWindowView
- (void) initializeWithDetails:(Details*)p_details
{
if(self != nil)
{
self.imageView.image = [UIImage imageWithContentsOfFile:p_basicDetails.imageURL];
self.label1.text = p_details.l1;
self.label2.text = p_details.l2;
}
}
-(IBAction) btn1_Clicked:(id)sender
{
NSLog(#"button clicked");
}
#end
and then in my view controller of the main screen and map
-(MarkerInfoWindowView*) customInfoWindow
{
if(_customInfoWindow == nil)
{
_customInfoWindow = [[[NSBundle mainBundle] loadNibNamed:#"MarkerInfoWindowView" owner:self options:nil] objectAtIndex:0];
}
return _customInfoWindow;
}
- (UIView *)mapView:(GMSMapView *)p_mapView markerInfoWindow:(GMSMarker *)p_marker
{
Details* temp = [[Details alloc] init];
temp.l1 = #"L1";
temp.l2 = #"L2";
temp.imageURL = #"someImage.jpg";
[self.customInfoWindow initializeWithDetails:temp];
return self.customInfoWindow;
}
any suggestions?
first the reason for the button not being clicked is because google-maps takes the UIView and renders it as an imageView so the button is part of an image and of course not clickable.
the solution is to add a UIView and handle on your own the hide/show and positioning.
instead of using
(UIView *)mapView:(GMSMapView *)p_mapView markerInfoWindow:(GMSMarker *)p_marker
i used didTapMarker and returned YES;