Save UIImageView When Unload A View - Xcode 4.5.1 - xcode

Creating an app on Xcode 4.5.1 and wondering how do i save the current image in the specified UIImageView when i navigate to a different view. Then when i return to the View it loads up the saved image into the UIImageView?
Thank a lot, appreciate it :D

The code I've posted below involves passing a uiimage to a second view controller. It also includes a button on each controller to move back and forth between the two. The saved image will stay in the uiimageview when you switch to and from the views.
SecondView.h
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController {
IBOutlet UIImageView *yourphotoview;
UIImage *yourimage;
}
#property (retain, nonatomic) UIImage *yourimage;
- (IBAction)back;
#end
SecondView.m
#import "ViewController.h"
#import "SecondView.h"
#interface SecondView ()
#end
#implementation SecondView
#synthesize yourimage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
yourphotoview.image = image;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)back {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)dealloc {
[image release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "SecondView.h"
#interface ViewController : UIViewController
{
IBOutlet UISlider *compression;
IBOutlet UISwitch *smurferderp;
SecondView *SecondViewdata;
}
#property (retain, nonatomic) SecondView *SecondViewdata;
#property (retain, nonatomic) IBOutlet UIImageView *theimage;
- (IBAction)switchview:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "SecondView.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize SecondViewdata, theimage;
- (IBAction)switchview:(id)sender {
SecondView *secondview = [self.storyboard instantiateViewControllerWithIdentifier:#"rr"];
self.SecondViewdata = secondview;
SecondViewdata.yourimage = theimage.image;
[self presentViewController: secondview animated:YES completion:NULL];
}
- (void)dealloc {
[theimage release];
[super dealloc];
}
#end

Related

Receiver is a forward class and corresponding #interface may not exist

thanks for taking the time to read my problem and help me:)
I am building a TabView project and on tab 3 of the app I have a row of albums, in album 3, page 1 the nextButton takes the user to page 2 of the album. Page 2 has more buttons, including a backPage(get back to page 1) and a backButton(to go back to the album selection page).
ERROR:
A3Page2ViewController.m:92:49: Property 'view' cannot be found in forward class object 'A3Page1ViewController'
WARNING:
A3Page2ViewController.m:91:28: Receiver 'A3Page1ViewController' is a forward class and corresponding #interface may not exist
I have searched this site and others and most of the answers are in relation to how #Class should be used in the header filw and #import ".h" should only be used in the .m file. I have checked my code and I was following the rules, so why does it still not work?
THIS IS WHAT I HAVE DONE: This is the function(in A3Page2ViewController) that is apparently making the error...
-(IBAction)backPage:(id)sender
{
a3Page1ViewController = [[A3Page1ViewController alloc]initWithNibName:#"A3Page1ViewController"bundle:nil];
[self.view addSubview:a3Page1ViewController.view];
}
Please note that when commented out, the app runs FINE all the views are loaded, all buttons including backButton work. When uncommented it just refuses to build, with the errors.
Below I list both A3Page1ViewController and A3Page2ViewController both the .h and .m files.
You can see that I have the #class and #import ".h" in the correct places.
WEIRDNESS!!
I actually have the exact same code WORKING in the 2nd tab, I use it to switch between views with out any problem. SO WHY DOES IT NOT WORK HERE?!?!
PLEASE HELP I AM STUCK ATM AND I DON'T KNOW WHY:/
A3Page1ViewController.h
#import <UIKit/UIKit.h>
#class A3Page2ViewController;
#class ThirdViewController;
#class A3P1;
#class A3P2;
#class A3P3;
#interface A3Page1ViewController : UIViewController {
A3Page2ViewController*a3Page2ViewController;
ThirdViewController*thirdViewController;
A3P1*A3P1;
A3P2*A3P2;
A3P3*A3P3;
UIButton *button1;
UIButton *button2;
UIButton *button3;
UIButton *nextButton;
UIButton *backButton;
}
#property(nonatomic,retain) IBOutlet A3Page2ViewController *a3Page2ViewController;
#property(nonatomic,retain) IBOutlet ThirdViewController *thirdViewController;
#property(nonatomic,retain) IBOutlet A3P1 *a3P1;
#property(nonatomic,retain) IBOutlet A3P2 *a3P2;
#property(nonatomic,retain) IBOutlet A3P3 *a3P3;
#property (nonatomic, retain) IBOutlet UILabel *logoLabel;
#property (nonatomic, retain) IBOutlet UILabel *descriptionLabel;
#property (nonatomic, retain) IBOutlet UILabel *copyrightLabel;
#property(nonatomic,retain) IBOutlet UIButton *button1;
#property(nonatomic,retain) IBOutlet UIButton *button2;
#property(nonatomic,retain) IBOutlet UIButton *button3;
#property(nonatomic,retain) IBOutlet UIButton *nextButton;
#property(nonatomic,retain) IBOutlet UIButton *backButton;
-(IBAction)FirstButton:(id)sender;
-(IBAction)SecondButton:(id)sender;
-(IBAction)ThirdButton:(id)sender;
-(IBAction)nextPage:(id)sender;
-(IBAction)backButton:(id)sender;
#end
A3Page1ViewController.m
#import "A3Page1ViewController.h"
#import "FXLabel.h"
#import <QuartzCore/QuartzCore.h>
#import "A3Page2ViewController.h"
#import "ThirdViewController.h"
#import "A3P1.h"
#import "A3P2.h"
#import "A3P3.h"
#implementation A3Page1ViewController
#synthesize a3Page2ViewController,thirdViewController,a3P1 ,a3P2 ,a3P3 , logoLabel, descriptionLabel, button1,button2,button3,nextButton,backButton, copyrightLabel;
-(UILabel*)createLabelWithFrame:(CGRect)frame andFontSize:(float)fontSize andText:(NSString*)text
{
UILabel* label = [[UILabel alloc] initWithFrame:frame];
[label setFont:[UIFont systemFontOfSize:fontSize]];
[label setTextColor:[UIColor whiteColor]];
[label setShadowColor:[UIColor blackColor]];
[label setShadowOffset:CGSizeMake(0, -1)];
//[label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:text];
return label;
}
-(IBAction)FirstButton:(id)sender
{
a3P1 = [[A3P1 alloc]initWithNibName:#"A3P1"bundle:nil];
[self.view addSubview:a3P1.view];
}
-(IBAction)SecondButton:(id)sender
{
a3P2 = [[A3P2 alloc]initWithNibName:#"A3P2"bundle:nil];
[self.view addSubview:a3P2.view];
}
-(IBAction)ThirdButton:(id)sender
{
a3P3 = [[A3P3 alloc]initWithNibName:#"A3P3"bundle:nil];
[self.view addSubview:a3P3.view];
}
-(IBAction)backButton:(id)sender
{
thirdViewController = [[ThirdViewController alloc]initWithNibName:#"ThirdView"bundle:nil];
[self.view addSubview:thirdViewController.view];
}
-(IBAction)nextPage:(id)sender
{
a3Page2ViewController = [[A3Page2ViewController alloc]initWithNibName:#"A3Page2ViewController"bundle:nil];
[self.view addSubview:a3Page2ViewController.view];
}
- (void)viewDidLoad
{
//CGRectMake(x,y,width,height);
// Do any additional setup after loading the view, typically from a nib.
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
A3Page2ViewController.h
#class A3Page1ViewController;
#class ThirdViewController;
#class A3P10;
#class A3P11;
#class A3P12;
#interface A3Page2ViewController : UIViewController {
A3Page1ViewController*a3Page1ViewController;
ThirdViewController*thirdViewController;
A3P10*a3P10;
A3P11*a3P11;
A3P12*a3P12;
UIButton *button1;
UIButton *button2;
UIButton *button3;
UIButton *backButton;
UIButton *backPage;
}
#property(nonatomic,retain) IBOutlet A3Page1ViewController *a3Page1ViewController;
#property(nonatomic,retain) IBOutlet ThirdViewController *thirdViewController;
#property(nonatomic,retain) IBOutlet A3P10 *a3P10;
#property(nonatomic,retain) IBOutlet A3P11 *a3P11;
#property(nonatomic,retain) IBOutlet A3P12 *a3P12;
#property(nonatomic,retain) IBOutlet UIButton *button1;
#property(nonatomic,retain) IBOutlet UIButton *button2;
#property(nonatomic,retain) IBOutlet UIButton *button3;
#property(nonatomic,retain) IBOutlet UIButton *backButton;
#property(nonatomic,retain) IBOutlet UIButton *backPage;
-(IBAction)FirstButton:(id)sender;
-(IBAction)SecondButton:(id)sender;
-(IBAction)ThirdButton:(id)sender;
-(IBAction)backButton:(id)sender;
-(IBAction)backPage:(id)sender;
#end
A3Page2ViewController.m
#import "A3Page1ViewController.h"
#import "FXLabel.h"
#import <QuartzCore/QuartzCore.h>
#import "A3Page2ViewController.h"
#import "ThirdViewController.h"
#import "A3P10.h"
#import "A3P11.h"
#import "A3P12.h"
#implementation A3Page2ViewController
#synthesize thirdViewController,a3Page1ViewController,a3P10 ,a3P11 ,a3P12, backButton,backPage, button1,button2,button3;
-(UILabel*)createLabelWithFrame:(CGRect)frame andFontSize:(float)fontSize andText:(NSString*)text
{
UILabel* label = [[UILabel alloc] initWithFrame:frame];
[label setFont:[UIFont systemFontOfSize:fontSize]];
[label setTextColor:[UIColor whiteColor]];
[label setShadowColor:[UIColor blackColor]];
[label setShadowOffset:CGSizeMake(0, -1)];
// [label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:text];
return label;
}
-(IBAction)FirstButton:(id)sender
{
a3P10 = [[A3P10 alloc]initWithNibName:#"A3P10"bundle:nil];
[self.view addSubview:a3P10.view];
}
-(IBAction)SecondButton:(id)sender
{
a3P11 = [[A3P11 alloc]initWithNibName:#"A3P11"bundle:nil];
[self.view addSubview:a3P11.view];
}
-(IBAction)ThirdButton:(id)sender
{
a3P12 = [[A3P12 alloc]initWithNibName:#"A3P12"bundle:nil];
[self.view addSubview:a3P12.view];
}
-(IBAction)backButton:(id)sender
{
thirdViewController = [[ThirdViewController alloc]initWithNibName:#"ThirdView"bundle:nil];
[self.view addSubview:thirdViewController.view];
}
-(IBAction)backPage:(id)sender
{
//*****Alleged Error/Warning causing code=[********
a3Page1ViewController = [[A3Page1ViewController alloc]initWithNibName:#"A3Page1ViewController"bundle:nil];
[self.view addSubview:a3Page1ViewController.view];
}
- (void)viewDidLoad
{
//CGRectMake(x,y,width,height);
// Do any additional setup after loading the view, typically from a nib.
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end

Impossibility to show in a label the text of an object

My objective is to show in a label the text of an object of a custom class called Files. Here is Files.h :
#import <Foundation/Foundation.h>
#interface Files : NSObject
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *text;
#end
This is Files.m :
#import "Files.h"
#implementation Files
#dynamic title;
#dynamic text;
#end
Here is the .h file of my app. the label is called trackName:
#import <UIKit/UIKit.h>
#import "Files.h"
#interface FirstViewController : UIViewController
{
Files *plainpalais;
}
#property (weak, nonatomic) IBOutlet UILabel *trackName;
-(Files*) chooseFile;
#end
This is the .m file of the app:
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize trackName;
-(Files*)chooseFile
{
return plainpalais;
}
- (void)viewDidLoad
{
[super viewDidLoad];
plainpalais.text=#"hello";
plainpalais.title=#"plainpalais";
trackName.text=plainpalais.title;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setTrackName:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
The problem is that the label trackName doesn't show plainpalais...
Thanks for help !
PS: I'm a beginner so this is probably a basic mistake.
You have used #dynamic in your Files.m implementation which tells the compiler that you'll provide getters/setters for these properties at a later time, i.e. using the Objective-C runtime.
I suspect you want to use #synthesize rather than #dynamic. For example,
#import "Files.h"
#implementation Files
#synthesize title;
#synthesize text;
#end
Also you haven't actually created a Files object in the code you have given us. The chooseFile method appears to be returning a nil object (assuming you haven't initialised plainpalais somewhere else). Perhaps you should initialise plainpalais in an init method, e.g.
- (id)init {
self = [super init];
if (self) {
plainpalias = [[Files alloc] init];
}
return self;
}
Don't forget to release this object in dealloc (if you aren't using ARC).

Semantic issue: Property 'text' not found on object of type 'UISlider'

When I try to run it though, I get an error which says "Semantic issue: Property 'text' not found on object of type 'UISlider'"
What's wrong here?
Here is the code from the header file:
#import <UIKit/UIKit.h>
#interface BIDViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *nameField;
#property (strong, nonatomic) IBOutlet UITextField *numberField;
#property (strong, nonatomic) IBOutlet UISlider *sliderLabel;
- (IBAction)textFieldDoneEditing:(id)sender;
- (IBAction)backgroundTap:(id)sender;
- (IBAction)sliderChanged:(id)sender;
#end
Implementation file:
#import "BIDViewController.h"
#implementation BIDViewController
#synthesize sliderLabel;
#synthesize nameField;
#synthesize numberField;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setNameField:nil];
[self setNumberField:nil];
[self setSliderLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
- (IBAction)backgroundTap:(id)sender
{
[nameField resignFirstResponder];
[numberField resignFirstResponder];
}
- (IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
int progressAsInt = (int)roundf(slider.value);
sliderLabel.text = [NSString stringWithFormat:#"%d",progressAsInt];
}
#end
I suspect your sliderLabel has been declared as an object of class UISlider, when it should be UILabel. Can you verify the sliderLabel's #property declaration in your header file?
I have the same message on NSManagedObject. My solution is set "Always Search User Paths" in "Search Paths" of target's Build Settings to "No"
Hope that could help too.

Can't call "MapAnnotation" When Trying To Code Pin Locations

I can't call the class "MapAnnotation" on XCode 4. I'm working on coding that will allow me to place pins in an MKMapView. I believe I've imported the right delegates. Here's what I've got:
MillersLocations.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#import <MapKit/MapKit.h>
#interface MillersLocations : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end
MillersLocations.m
#import "MillersLocations.h"
#implementation MillersLocations
#synthesize coordinate, title, subtitle;
-(void)dealloc{
[title dealloc];
[subtitle dealloc];
[super dealloc];
}
#end
And here's my view controller for the map view:
MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MapViewController : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *mapView;
}
#end
MapViewController.m (just the segment I'm looking at)
#import "MapViewController.h"
#import "MillersLocations.h"
#implementation MapViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
//skipping forward
- (void)viewDidLoad
{
[super viewDidLoad];
MKCoordinateRegion store1;
store1.center.latitude = 36.8605679;
store1.center.longitude = -76.2866713;
store1.span.latitudeDelta = 0.1;
store1.span.longitudeDelta = 0.1;
[mapView setRegion:store1 animated:YES];
//this is where I'm trying to put this code in:
MapAnnotation* annotation = [[MapAnnotation alloc] initWithCoordinate:newCoord];
//BUT "MapAnnotation" isn't an option
}
I'm wondering if I haven't imported the rights classes or something. I've Googled it and can't seem to find where "MapAnnotation" lies. What do I need to import to get access to "MapAnnotation"? Everything works fine up until that point.
Thanks for the help. I'm just learning this stuff!
What makes you think that there is class called MapAnnotation? Your annotation class is called MillersLocations. You need to create instances of that class and call [mapView addAnnotation:millersLocationInstance]; (or something similar).

Why won't this simple view switching project work?

I'm in the process of learning how to write apps in Xcode. I'm using a book to guide me. Unfortunately the book is written with guides to Xcode 3 and I'm using Xcode 4.
Now so far there haven't been any problems, but this project doesn't work, and I simply don't get it, because it seems to make pretty good sense.
The project's goal is to use a view controller to switch between three views.
Could anyone please take a look and see what I did wrong?
Here is the entire project: http://www.2shared.com/file/CKO6ACzg/MultipleViews.html
PS: I know that as it is now the views will be stacked on top off each other and that the view isn't being cleared when you click a new button.
MultipleViewsViewController.h should be:
#import <UIKit/UIKit.h>
#class FirstViewController;
#class SecondViewController;
#class ThirdViewController;
#interface MultipleViewsViewController : UIViewController {
IBOutlet FirstViewController *firstViewController;
IBOutlet SecondViewController *secondViewController;
IBOutlet ThirdViewController *thirdViewController;
}
//#property (nonatomic, retain) FirstViewController *firstViewController;
//#property (nonatomic, retain) SecondViewController *secondViewController;
//#property (nonatomic, retain) ThirdViewController *thirdViewController;
-(IBAction)loadFirstView:(id)sender;
-(IBAction)loadSecondView:(id)sender;
-(IBAction)loadThirdView:(id)sender;
#end
MultipleViewsViewController.m should be:
#import "MultipleViewsViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#implementation MultipleViewsViewController
//#synthesize firstViewController;
//#synthesize secondViewController;
//#synthesize thirdViewController;
-(IBAction)loadFirstView:(id)sender{
[secondViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:firstViewController.view atIndex:0];
}
-(IBAction)loadSecondView:(id)sender{
[firstViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:secondViewController.view atIndex:0];
}
-(IBAction)loadThirdView:(id)sender{
[firstViewController.view removeFromSuperview];
[secondViewController.view removeFromSuperview];
[self.view insertSubview:thirdViewController.view atIndex:0];
}
-(void)dealloc{
[firstViewController release];
[secondViewController release];
[thirdViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
firstViewController = [[FirstViewController alloc] init];
secondViewController = [[SecondViewController alloc] init];
thirdViewController = [[ThirdViewController alloc] init];
[self loadFirstView:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Connect your buttons (which you haven't done in your project, which also might be the issue) and you're done.

Resources