.h
IBOutlet UIImageView *photoImageView;
UIImage *photo;
.m (in viewWillAppear)
photoImageView.image = photo;
After view appeared, I can't change photoImageView by using:
photoImageView.image = [UIImage imageNamed:#"XXX.png"];
but changing the value of photo works.
photo = [UIImage imageNamed:#"XXX.png"];
How does it work?
Try this code.
//YourController.h
#interface YourController : UIViewController
{
UIImageView* _photoImageView;
}
#property (retain, nonatomic) IBOutlet UIImageView *photoImageView;
- (IBAction)changeImageAction:(id)sender;
#end
//YourController.m
#synthesize photoImageView = _photoImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.photoImageView.image = [UIImage imageNamed:#"img.png"];
}
- (IBAction)changeImageAction:(id)sender {
self.photoImageView.image = [UIImage imageNamed:#"img_2.png"];
}
Some notes.
First of all, you have to link your photoImageView IBOutlet correctly in XCode. For test purposes I created a test button (UIButton) that is linked to an IBAction. The UIImageView and the test button (I created both with IB) are childs of YourController view.
Remember to import your images into your project and to release memory in dealloc and in viewDidUnload.
- (dealloc)
{
[_photoImageView release];
_photoImageView = nil;
[super dealloc]
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.photoImageView = nil;
}
Hope it helps.
Edit
The code is not ARC oriented but with few modifications it is possible to run it also with ARC.
Related
Hi i am very new for ios and in my project i am using PageViewController
for displaying and swiping the images,Ok that's fine
But here i am loading images on my pageViewcontroller from services(i mean url images i am loading)
For this i am using SDWebImage library for loading images,But here i have underlined clearly where i am getting errors below in my RootViewController.m please help me how can i load images in PageViewController using SDWebImage library and what is the wrong in that line please help me
my code:-
PageContentViewController.h
#interface PageContentViewController: UIViewController
#property (weak,nonatomic) IBOutlet UIImageView * ivScreenImage;
#property NSUInteger pageIndex;
#property NSString *imgFile;
#property NSString *txtTitle;
#end
PageContentViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.ivScreenImage.image = [UIImage imageNamed:self.imgFile];
}
RootViewController.h
#import <SDWebImage/UIImageView+WebCache.h>
#interface RootViewController : UIViewController
<UIPageViewControllerDataSource>
#property (nonatomic,strong) UIPageViewController *PageViewController;
#property (nonatomic,strong) NSArray *arrPageImages;
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index;
#end
RootViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
arrPageImages =#[#"http://www.domain.com/path/to/image1.jpg",#"http://www.domain.com/path/to/image2.jpg",#"http://www.domain.com/path/to/image3.jpg"];
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index{
if (([self.arrPageTitles count] == 0) || (index >= [self.arrPageTitles count])) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
pageContentViewController.imgFile setImageWithURL:[[[NSURL URLWithString:[self.arrPageImages[index]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
--------------------------------------------------------------------------------
pageContentViewController.pageIndex = index;
return pageContentViewController;
}
SDWebImage method for setting image for UIImageView is
sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
also imgFile variable should be UIImageView.
I'm developing an iOS application and I have a strange problem.
The application sends local notifications to the user and when he taps on it, my app programmatically chooses a ViewController, sets its labels and images and then displays it to the user. But it simply doesn't work! Labels and images are not set. Here's my code in AppDelegate.m
- (void) manageLocalNotification:(UILocalNotification *) notification {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
if ([self.theme intValue] == 1) {
ADATheme1ViewController *notifyViewController = [[ADATheme1ViewController alloc] init];
[notifyViewController.titleLabel setText:self.title];
[notifyViewController.bodyLabel setText:self.text];
[notifyViewController.bgImage setImage: [self getImageFromURL:self.background]];
self.window.rootViewController = notifyViewController;
} else {
ADATheme2ViewController *notifyViewController = [[ADATheme2ViewController alloc] init];
[notifyViewController.titleLabel setText:self.title];
[notifyViewController.bodyLabel setText:self.text];
[notifyViewController.offerLabel setText:self.offer];
[notifyViewController.bgImage setImage: [self getImageFromURL:self.background]];
self.window.rootViewController = notifyViewController;
}
}
I made some debugging and my properties (text, title, offer and so on) are properly set. The method getImageFromUrl: returns an UIImage* and it works.
My ViewControllers are very simple, here's the code (but nothing special)
.h file
#import
#interface ADATheme1ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *bgImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UILabel *bodyLabel;
#end
.m file
#import "ADATheme1ViewController.h"
#interface ADATheme1ViewController ()
#end
#implementation ADATheme1ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
#end
and there's a .xib file attached.
Can you help me?
Solved: the view weren't loaded so the properties were set to nil.
I added just a line of code to "force" the loading of my view before setting labels:
[notifyViewController view];
Writing my first iphone app through this tutorial--I have a custom UIView with the method showDie and I have two UIView elements on my storyboard that I've hooked up to my ViewController. However, both the UIView elements have the error message "No visible #interface for 'UIView' declares the selector 'showDie'. I think that's because the UIViews aren't subclassing XYZDieView, but when I control-clicked from my Storyboard, only UIView appeared in the dropdown--no XYZDieView. I tried just changing the text manually in the ViewController.h, but that didn't work (I didn't think it would). Am I on the right track? How do I use my subclass? (xcode 5)
XYZDieView.m
-(void)showDie:(int)num
{
if (self.dieImage == nil){
self.dieImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
[self addSubview:self.dieImage];
}
NSString *fileName = [NSString stringWithFormat:#"dice%d.png", num];
self.dieImage.image = [UIImage imageNamed:fileName];
}
XYZViewController.h
#import <UIKit/UIKit.h>
#import "XYZDieView.h"
#interface XYZViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *sumLabel;
#property (strong, nonatomic) IBOutlet UIButton *rollButton;
#property (strong, nonatomic) IBOutlet UIView *leftDieView;
#property (strong, nonatomic) IBOutlet UIView *rightDieView;
#end
XYZViewController.m
#import "XYZViewController.h"
#import "XYZDiceDataController.h"
#interface XYZViewController ()
#end
#implementation XYZViewController
- (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)rollButtonClicked:(UIButton *)sender {
XYZDiceDataController *diceDataController = [[XYZDiceDataController alloc] init];
int roll = [diceDataController getDiceRoll];
int roll2 = [diceDataController getDiceRoll];
[self.leftDieView showDie:roll];
[self.rightDieView showDie:roll2];
int sum = roll + roll2;
NSString *sumText = [NSString stringWithFormat:#"Sum is %i", sum];
self.sumLabel.text = sumText;
}
#end
Yes you're right, the problem is subclassing. In order to have a view conform to a subclass in the interface builder you need to
Click on the UIView in your storyboard
Open the right inspector panel
Click on the third tab for identity inspector
Add your class
Then try to connect it to your ViewController again.
Edit: Afterwards you may need to cast XYZDieView myView = (XYZDieView*)leftDieView;
I am doing a little self learning with Obj-c and interested in making a flash card application. I have an NSArray with objects with 5 images in the array. When the user presses the forward button I am trying to get my UIImageView to show the next image in the array. I get Thread 1: EXC_BAD_ACCESS(Code=2,address=0x0) on the line where I get an image from my array. Here is my current header and implementation file.
#interface WhoAmIViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *flashcardviewer;
#property (strong, nonatomic) NSArray *aryFlashcards;
#property (nonatomic) NSUInteger *int_counter;
#end
#implementation WhoAmIViewController
#synthesize aryFlashcards;
#synthesize flashcardviewer;
#synthesize int_counter;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aryFlashcards = [NSArray arrayWithObjects:
[UIImage imageNamed:#"shot_1"],
[UIImage imageNamed:#"shot_2"],
[UIImage imageNamed:#"shot_3"],
[UIImage imageNamed:#"shot_4"],
[UIImage imageNamed:#"shot_5"],
nil];
int_counter = 0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//back button
- (IBAction)btn_back:(UIButton *)sender {
}
//forward button
- (IBAction)btn_forward:(UIButton *)sender
{
flashcardviewer = [[UIImageView alloc]init];
UIImage *flashcard_next = [UIImage imageNamed:[aryFlashcards objectAtIndex:*(int_counter)]]; //This is where Xcode throws me an error.
flashcardviewer.image = flashcard_next;
int_counter++;
}
//random button
- (IBAction)btn_random:(UIButton *)sender
{
}
You made int_counter a pointer instead of an NSInteger. Try
#property (nonatomic) NSUInteger int_counter;
and
UIImage *flashcard_next = [aryFlashcards objectAtIndex:int_counter];
Try the following code:
//Declare int_counter like this
#property (nonatomic) NSUInteger int_counter;
//Set btn_forward: this way
- (IBAction)btn_forward:(UIButton *)sender
{
//Avoid possible app crash with a if condition
if (int_counter < flashcard_next.count) {
UIImage *flashcard_next = [aryFlashcards objectAtIndex:int_counter];
flashcardviewer.image = flashcard_next;
int_counter++;
} else {
NSLog (#"No more cards");
}
}
You can learn more about NSInteger here.
Your problem is on this line:
UIImage *flashcard_next = [UIImage imageNamed:[aryFlashcards objectAtIndex:*(int_counter)]];
You're creating a UIImage object and passing another UIImage to the method when it's expecting a NSString. Change this to:
UIImage *flashcard_next = [aryFlashcards objectAtIndex:int_counter];
and as mentioned in the other answer, remove the pointer to int_counter.
Your iVar aryFlashcards is already instantiated with 5 images in viewDidLoad method. In your btn_forward method implementation you should replace
UIImage *flashcard_next = [UIImage imageNamed:[aryFlashcards objectAtIndex:*(int_counter)]];
With following provided *(int_counter) is less than aryFlashCards size which looks to be 5 in your example else your app will throw out of bound exception.
UIImage *flashcard_next = [aryFlashcards objectAtIndex:*(int_counter)];
I've really been struggling, maybe since I'm a newbie into Objective-C, to be able to perform the following.
Controller MAViewControllerMenu (controller 1) holds a button that allows to select a picture from the library
Controller MAViewControllerPictureDisplay (controller 2) holds an imageView (bigImageView)
Once I select a picture from the picker in controller 1, I want to move to the next window (controller 2) and there, have the imageView holding my selected picture.
Problems:
1. It wouldn't show the next controller
2. it wouldn't change the imageView to hold my selected picture
Observations:
1. When I have the imageView on controller 1 rather than 2, it holds the picture just like I want it to
2. The debugger shows that the function to change imageView in controller 2's class is invoked. However, nothing changes, even when I tried to simply change a label.
Code:
MAViewControllerMenu.h
#interface MAViewControllerMenu : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImagePickerController *picker1;
UIImagePickerController *picker2;
UIImage *image;
IBOutlet UIImageView *imageView;//THIS CHANGES,BUT IS IN CONTROLLER 1
}
- (IBAction)TakePhoto;
- (IBAction)ChooseExisting;
- (IBAction)movetonext;
MAViewControllerPictureDisplay.h
#import "MAViewControllerMenu.h"
#interface MAViewControllerPictureDisplay : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate,menuControllerDelegate>
{
}
#property (weak, nonatomic) IBOutlet UIImageView *bigImageView;//THIS ONE WOULDNT HOLD THE CHOSEN IMAGE
#property (weak, nonatomic) IBOutlet UILabel *theLabel;
#property(nonatomic, strong) UIImage *image;
#end
implementation
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//load next page
self.bigImageView.image = self.image;
}
method to react to picture selection at MAViewControllerMenu.m
#import "MAViewControllerMenu.h"
#import "MAViewControllerPictureDisplay.h"
#interface MAViewControllerMenu ()
#end
#implementation MAViewControllerMenu
MAViewControllerPictureDisplay* imageControllerView;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image]; //WORKS ON CURRENT CONTROLLER
//imageControllerView.theLabel.text = #"hhdhdhd"; //DIDNT WORK
[imageControllerView.bigImageView setImage:image]; //UPDATE IMAGE AT CONTROLLER 2, DIDNT WORK
[self dismissViewControllerAnimated:YES completion:NULL];
[self movetonext];
}
- (IBAction)movetonext{
[self presentViewController:imageControllerView animated:YES completion:nil];//MOVE TO NEXT WINDOW- DOESNT WORK WHEN INVOKED AFTER SELECTING A PICTURE. DOES WORK WHEN INVOKING,SAY, AFTER A BUTTON WAS CLICKED
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//load next page
imageControllerView = [[self storyboard] instantiateViewControllerWithIdentifier:#"chosenImageController"];
}
The picture does not show up in the second view controller because the image view you are setting an image does not exist yet at this point.
[imageControllerView.bigImageView setImage:image];
Declare a UIImage property and pass the image using it. Then in imageControllerView's viewDidLoad show it in a UIImageView. Example:
#property(nonatomic, strong) UIImage *image;
- (void)viewDidLoad
{
[super viewDidLoad];
_bigImageView.image = _image;
}
Don't do that. You should treat another view controller's views as private. Reecon shows the correct way to do this (set up a property to hold the information, then display that info in viewDidLoad or vieWillAppear:animated)