Image is not loading on UIImageView - objective-c

I am showing data in a UITableView with a thumbnail image. Initially, when the cell is clicked on, I would reload the image from the server which would take additional time, resources, and memory. Instead of doing this, I thought of passing a pointer of the thumbnail image to the view that will be on screen and display it immediately, instead of having to redownload it. However, I am running into a problem - nothing loads when I set the imageView on the new view to the old image. Here is my code:
This is in my UITableView Class:
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
NewsViewController *newsViewController = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil dictionary: dict type:d numberOfPages:1 image: cell.imageView.image];
NewsViewController.m:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dictionary:(NSDictionary *)dict type:(NSInteger)type numberOfPages:(NSInteger)pages image:(UIImage *)image{
self = [super initWithNibName:#"NewsViewController" bundle:Nil];
if (self) {
newsText = [dict objectForKey:#"content"];
newsTitle = [dict objectForKey:#"title"];
newsDate = [dict objectForKey:#"pub_date"];
d = type;
numberOfPages = pages;
imageURL = [[NSMutableArray alloc] initWithCapacity:numberOfPages];
mainImage = [[UIImage alloc] init];
mainImage = image;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
labelBody.text= newsText;
titleView.text = newsTitle;
date.text = newsDate;
if (numberOfPages == 1) {
imageScrollView.contentSize = CGSizeMake(320, 303);
pageControl.numberOfPages = 1;
CustomImageView *customImageView = [[CustomImageView alloc] initWithFrame:imageScrollView.bounds];
[imageScrollView addSubview:customImageView];
customImageView.imageView.image = mainImage;
}
}
NewsViewController.h
#import <UIKit/UIKit.h>
#import "AdWhirlView.h"
#import "AdWhirlDelegateProtocol.h"
#class Reachability;
#interface NewsViewController : UIViewController<AdWhirlDelegate, UIScrollViewDelegate>{
AdWhirlView *adView;
IBOutlet UILabel *labelBody;
IBOutlet UILabel *titleView;
IBOutlet UILabel *subTitle;
IBOutlet UILabel *date;
IBOutlet UIScrollView *scrollView;
IBOutlet UIScrollView *imageScrollView;
IBOutlet UIImageView *backgroundImage;
IBOutlet UIImage *mainImage;
IBOutlet UIPageControl *pageControl;
NSInteger numberOfPages;
NSMutableArray *imageURL;
}
#property(nonatomic,retain)AdWhirlView *adView;
#property (nonatomic, retain) NSMutableArray *imageURL;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) IBOutlet UIScrollView *imageScrollView;
#property (nonatomic, weak) IBOutlet UIImageView *backgroundImage;
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIImage *mainImage;
#property (nonatomic, strong) NSString *newsText;
#property (nonatomic, strong) NSString *newsTitle;
#property (nonatomic, strong) NSString *newsDate;
#property (nonatomic, assign) NSInteger numberOfPages;
#property (nonatomic, strong) IBOutlet UILabel *titleView;
#property (nonatomic, strong) IBOutlet UILabel *labelBody;
#property (nonatomic, strong) IBOutlet UILabel *subTitle;
#property (nonatomic, strong) IBOutlet UILabel *date;
#property (nonatomic, retain) Reachability* internetReachable;
#property (nonatomic, assign) BOOL internetActive;
-(void) checkNetworkStatus:(NSNotification *)notice;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dictionary: (NSDictionary *)dict type:(NSInteger)type numberOfPages:(NSInteger) pages image:(UIImage *)image;
- (BOOL)checkInternet;
- (void)doStuff;
- (IBAction)ad;
#end
I am using mainImage as a (UIImage *) and trying to replace the image in my customImageView class with the main Image but nothing is showing up. What am I missing here?

I guess you'd like to build some sort of caching mechanism for your UIImageView. One of the easiest ways to get all of this working is by making use of the AFNetworking library.
AFNetworking extends UIImageView with some extra methods for loading images from remote sources. AFNetworking has caching build-in, no need to reinvent the wheel :)
Take a look on the following sites:
AFNetworking
UIImageView (AFNetworking)

Related

Passing map-data between ViewController

I nearly followed this post. The viewcontroller will push via storyboard. Here the relevant code:
ViewController.m:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"%#", view.annotation.title);
MapPoint *annView = view.annotation;
DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dvc.title = #"my own Details";
dvc.titleTMP = annView.title;
dvc.subtitleTMP = annView.subtitle;
[self.navigationController pushViewController:dvc animated:YES];
}
MapPoint.h:
#interface MapPoint : NSObject <MKAnnotation>
{
NSString *_title;
NSString *_subtitle;
CLLocationCoordinate2D _coordinate;
UIImage *_storeImage;
}
#property (copy) NSString *title;
#property (copy) NSString *subtitle;
#property (nonatomic, readonly) UIImage *storeImage;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString*)title subtitle:(NSString*)subtitle coordinate:(CLLocationCoordinate2D)coordinate storeImage:(UIImage*)storeImage;
and DetailViewController:
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *branchImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UITextView *textView;
I think my mistake is in the calloutAccessoryControlTapped method but I dont know what is the real reason for this issue.
If you use a storyboard, can use prepareForSegue method and set within a class similarly as you have made.
I post a portion of code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"Brs"]){
NSLog(#"segue %# ",[segue identifier]);
[segue.destinationViewController setPath:[ris_xml objectAtIndex:index_post-1] ];
}
}
In this example I set attribute "Path" of next UIViewController only if his identifier is "Brs".
For use this method is need set UIviewController identifier into storyboard right panel.
Using this isn't need to instantiate new UIViewController if you have it in storyboard.
I solved it by thinking about loading sequence.
All what to do is create temporary #properties
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *branchImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UITextView *textView;
#property (nonatomic) NSString *titleTMP; //added
#property (nonatomic) NSString *subtitleTMP; //added
and do
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"%#", view.annotation.title);
MapPoint *annView = view.annotation;
DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dvc.title = #"my own Details";
dvc.titleTMP = annView.title;
dvc.subtitleTMP = annView.subtitle;
[self.navigationController pushViewController:dvc animated:YES];
}
The DetailViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
titleLabel.text = titleTMP;
textView.text = subtitleTMP;
}

populate uipickerview from sqlite

I have a uipickerview which i want to populate with data from my sqlite database.
I have got access to the data via a mutable array via the appdelegate but when i use that as the data source it is crashing the app when used as the uipicker titleforrow section.
The count from the array is correct it just will not display any data!
Here is my header file:
#class OBMessage;
#interface LocationViewController : UIViewController <UITableViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDataSource, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate> {
UITableViewCell *cell1;
UITableViewCell *cell2;
UITableViewCell *cell3;
UITableViewCell *cell4;
UITableViewCell *cell5;
UITableViewCell *cell6;
UITableViewCell *cell7;
UITableViewCell *cell8;
IBOutlet UILabel *addressLabel;
MKPlacemark *placemark;
IBOutlet UILabel *lLat;
IBOutlet UILabel *lLong;
IBOutlet UILabel *lHAcc;
IBOutlet UILabel *lAlt;
IBOutlet UILabel *lVAcc;
IBOutlet UILabel *lDate;
UIPickerView *singlePicker;
NSArray *pickerData;
NSString *pickedInfo;
DadsTaxiV2AppDelegate *appDelegate;
NSMutableArray *myarray;
}
#property (nonatomic, retain) IBOutlet UITableViewCell *cell1;
#property (nonatomic, retain) IBOutlet UITableViewCell *cell2;
#property (nonatomic, retain) IBOutlet UITableViewCell *cell3;
#property (nonatomic, retain) IBOutlet UITableViewCell *cell4;
#property (nonatomic, retain) IBOutlet UITableViewCell *cell5;
#property (nonatomic, retain) IBOutlet UITableViewCell *cell6;
#property (nonatomic, retain) IBOutlet UITableViewCell *cell7;
#property (nonatomic, retain) IBOutlet UITableViewCell *cell8;
#property (nonatomic, retain) IBOutlet UILabel *addressLabel;
#property (nonatomic, retain) IBOutlet UILabel *lLat;
#property (nonatomic, retain) IBOutlet UILabel *lLong;
#property (nonatomic, retain) IBOutlet UILabel *lHAcc;
#property (nonatomic, retain) IBOutlet UILabel *lAlt;
#property (nonatomic, retain) IBOutlet UILabel *lVAcc;
#property (nonatomic, retain) IBOutlet UILabel *lDate;
#property (nonatomic, retain) IBOutlet UIPickerView *singlePicker;
#property (nonatomic, retain) NSArray *pickerData;
#property (nonatomic, retain) NSString *pickedInfo;
#property (nonatomic, retain) NSMutableArray *myarray;
#property (nonatomic, retain) MKPlacemark *placemark;
-(IBAction)popupCameraSheet:(id)sender;
-(IBAction)popupMessageSheet:(id)sender;
-(void)showPicker;
#end
Here is bits of my m file:
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (DadsTaxiV2AppDelegate *)[[UIApplication sharedApplication] delegate];
self.myarray = appDelegate.messageArray;
}
-(void)showPicker
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Select a Office Buddy Message to Include:"
delegate:self
cancelButtonTitle:#"Select" destructiveButtonTitle:#"Cancel"
otherButtonTitles:nil];
menu.tag = 4;
menu.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
singlePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 185, 0, 0)];
singlePicker.delegate=self;
singlePicker.showsSelectionIndicator = YES;
[singlePicker selectedRowInComponent:0];
[singlePicker reloadComponent:0];
[menu addSubview:singlePicker];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0, 0, 320, 700)];
[singlePicker release];
[menu release];
//NSInteger row = [singlePicker selectedRowInComponent:1];
//NSString *selected = [pickerData objectAtIndex:row];
//infoLabel.text = selected;
}
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//NSLog(#"[pickerData count]=%d",[pickerData count]);
//return [pickerData count];
NSLog(#"[messagearray count]=%d",[self.myarray count]);
//return [self.pickerData count];
return [self.myarray count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(#"[pickerData objectAtIndex:row]=%#",[self.myarray objectAtIndex:row]);
//return [self.pickerData objectAtIndex:row];
return [self.myarray objectAtIndex:row];
}
Sorry forgot the crash log:
didSelectViewController:<LocationViewController: 0x9b7de90>
2012-09-07 20:43:07.142 OfficeBuddy[7300:c07] [messagearray count]=13
2012-09-07 20:43:07.142 OfficeBuddy[7300:c07] [messagearray count]=13
2012-09-07 20:43:07.142 OfficeBuddy[7300:c07] [pickerData objectAtIndex:row]=<OBMessage: 0x9a74ae0>
2012-09-07 20:43:07.143 OfficeBuddy[7300:c07] -[OBMessage isEqualToString:]: unrecognized selector sent to instance 0x9a74ae0
2012-09-07 20:43:07.144 OfficeBuddy[7300:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[OBMessage isEqualToString:]: unrecognized selector sent to instance 0x9a74ae0'
*** First throw call stack:
(0x1759022 0x1a5ecd6 0x175acbd 0x16bfed0 0x16bfcb2 0x8ed0ff 0xbcf17a 0x7a8bc0 0x847c54 0x8483ce 0x833cbd 0x83271a 0x83c220 0xbcf6bb 0x7a77ec 0x7a84f9 0x8ca1 0xcc71 0x83e5c5 0x83e7fa 0x47585d 0x172d936 0x172d3d7 0x1690790 0x168fd84 0x168fc9b 0x22847d8 0x228488a 0x7ad626 0x2f5d 0x2ed5)
terminate called throwing an exception(lldb)
By your crash log, it seems that the data you are populating your array with is an instance of the OBMessage class. When returning from the datasource method:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
Make sure you are returning a string. So, if your array is populated by instances of the OBMessage, use a property from that class that represents what you want to present on your picker. For example, if OBMessage has a property called messageStringRepresentation, you would return:
return [[self.myarray objectAtIndex:row] messageStringRepresentation];
Try to print the array objects and confirm NSString objects. or post a code if you have used isEqualToString somewhere in your code.

Having trouble declaring/initializing array of CGPoints

Like most of my problems recently, i think it's a syntax problem. I know what i want to do but i don't know how to write it. I am trying to create a Jigsaw-type application. The CGPoints represent the center point of where each image should be.
I have created 40 images and put them into an array.
I want to create 40 CGPoints and put all these points in an array.
The image at images[0] has a corresponding center at cgpoints[0] - or rather this is where the image should be placed in the jigsaw.
I'm having trouble declaring/initializing the array of CGPoints though. Below is the code:
.h file
#import <UIKit/UIKit.h>
#interface DSAAssignmentViewController2 : UIViewController
#property (weak, nonatomic) NSString *passedData;
#property (weak, nonatomic) IBOutlet UILabel *lblDisplayPreviousInput;
#property (weak, nonatomic) IBOutlet UIImageView *imgOriginal;
#property (weak, nonatomic) IBOutlet UIImageView *img1;
#property (weak, nonatomic) IBOutlet UIImageView *img2;
#property (weak, nonatomic) IBOutlet UIImageView *img3;
#property (weak, nonatomic) IBOutlet UIImageView *img4;
#property (weak, nonatomic) IBOutlet UIImageView *img5;
#property (weak, nonatomic) IBOutlet UIImageView *img6;
#property (weak, nonatomic) IBOutlet UIImageView *img7;
#property (weak, nonatomic) IBOutlet UIImageView *img8;
#property (weak, nonatomic) IBOutlet UIImageView *img9;
#property (weak, nonatomic) IBOutlet UIImageView *img10;
#property (weak, nonatomic) IBOutlet UIImageView *img11;
#property (weak, nonatomic) IBOutlet UIImageView *img12;
#property (weak, nonatomic) IBOutlet UIImageView *img13;
#property (weak, nonatomic) IBOutlet UIImageView *img14;
#property (weak, nonatomic) IBOutlet UIImageView *img15;
#property (weak, nonatomic) IBOutlet UIImageView *img16;
#property (weak, nonatomic) IBOutlet UIImageView *img17;
#property (weak, nonatomic) IBOutlet UIImageView *img18;
#property (weak, nonatomic) IBOutlet UIImageView *img19;
#property (weak, nonatomic) IBOutlet UIImageView *img20;
#property (weak, nonatomic) IBOutlet UIImageView *img21;
#property (weak, nonatomic) IBOutlet UIImageView *img22;
#property (weak, nonatomic) IBOutlet UIImageView *img23;
#property (weak, nonatomic) IBOutlet UIImageView *img24;
#property (weak, nonatomic) IBOutlet UIImageView *img25;
#property (weak, nonatomic) IBOutlet UIImageView *img26;
#property (weak, nonatomic) IBOutlet UIImageView *img27;
#property (weak, nonatomic) IBOutlet UIImageView *img28;
#property (weak, nonatomic) IBOutlet UIImageView *img29;
#property (weak, nonatomic) IBOutlet UIImageView *img30;
#property (weak, nonatomic) IBOutlet UIImageView *img31;
#property (weak, nonatomic) IBOutlet UIImageView *img32;
#property (weak, nonatomic) IBOutlet UIImageView *img33;
#property (weak, nonatomic) IBOutlet UIImageView *img34;
#property (weak, nonatomic) IBOutlet UIImageView *img35;
#property (weak, nonatomic) IBOutlet UIImageView *img36;
#property (weak, nonatomic) IBOutlet UIImageView *img37;
#property (weak, nonatomic) IBOutlet UIImageView *img38;
#property (weak, nonatomic) IBOutlet UIImageView *img39;
#property (weak, nonatomic) IBOutlet UIImageView *img40;
#property (strong, nonatomic) NSArray *imageViews;
#property CGPoint *holder1;
#property CGPoint *holder2;
#property CGPoint *holder3;
#property CGPoint *holder4;
#property CGPoint *holder5;
#property CGPoint *holder6;
#property CGPoint *holder7;
#property CGPoint *holder8;
#property (strong, nonatomic) NSMutableArray *holders;
#end
.m file
#import "DSAAssignmentViewController2.h"
#interface DSAAssignmentViewController2 ()
#end
#implementation DSAAssignmentViewController2
#synthesize lblDisplayPreviousInput;
#synthesize imgOriginal;
#synthesize img1;
#synthesize img2;
#synthesize img3;
#synthesize img4;
#synthesize img5;
#synthesize img6;
#synthesize img7;
#synthesize img8;
#synthesize img9;
#synthesize img10;
#synthesize img11;
#synthesize img12;
#synthesize img13;
#synthesize img14;
#synthesize img15;
#synthesize img16;
#synthesize img17;
#synthesize img18;
#synthesize img19;
#synthesize img20;
#synthesize img21;
#synthesize img22;
#synthesize img23;
#synthesize img24;
#synthesize img25;
#synthesize img26;
#synthesize img27;
#synthesize img28;
#synthesize img29;
#synthesize img30;
#synthesize img31;
#synthesize img32;
#synthesize img33;
#synthesize img34;
#synthesize img35;
#synthesize img36;
#synthesize img37;
#synthesize img38;
#synthesize img39;
#synthesize img40;
#synthesize passedData;
#synthesize imageViews;
#synthesize holder1;
#synthesize holder2;
#synthesize holder3;
#synthesize holder4;
#synthesize holder5;
#synthesize holder6;
#synthesize holder7;
#synthesize holder8;
#synthesize holders;
- (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.
// make jigsaw piece holders
//create 8 points where the jigsaw pieces (images) will be placed
CGPoint holder1 = CGPointMake(60, 644);
CGPoint holder2 = CGPointMake(140, 644);
CGPoint holder3 = CGPointMake(220, 644);
CGPoint holder4 = CGPointMake(300, 644);
CGPoint holder5 = CGPointMake(380, 644);
CGPoint holder6 = CGPointMake(460, 644);
CGPoint holder7 = CGPointMake(540, 644);
CGPoint holder8 = CGPointMake(620, 644);
// set up array of cgpoints
holders = [NSMutableArray arrayWithObjects:[NSValue valueWithCGPoint: holder1],[NSValue valueWithCGPoint: holder2], [NSValue valueWithCGPoint: holder3], [NSValue valueWithCGPoint: holder4], [NSValue valueWithCGPoint: holder5], [NSValue valueWithCGPoint: holder6], [NSValue valueWithCGPoint: holder7], [NSValue valueWithCGPoint: holder8], nil];
// set up array of images
imageViews = [NSArray arrayWithObjects:img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16, img17, img18, img19, img20, img21, img22, img23, img24, img25, img26, img27, img28, img29, img30, img31, img32, img33, img34, img35, img36, img37, img38, img39, img40, nil];
NSString *welcomeMessage = [NSString stringWithFormat:#"Welcome %#", passedData];
UIAlertView *alert = [[UIAlertView alloc] //show alert box with option to play or exit
initWithTitle: welcomeMessage
message:#"Once you press \"Play\" the timer will start. Good luck!"
delegate:self
cancelButtonTitle:#"I want out!"
otherButtonTitles:#"Play",nil];
[alert show];
NSLog(#"x-coord: %f, y-coord: %f", holder1.x, holder1.y);
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
UIView *touchedView = [touch view];
if ([imageViews indexOfObject:touchedView] != NSNotFound) {
// not not found means found!
[self animateFirstTouch:touchedView withLocation:location];
} else {
return;
}
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
UIView *touchedView = [touch view];
[self animateReleaseTouch:touchedView withLocation:location];
}
- (void)viewDidUnload
{
[self setLblDisplayPreviousInput:nil];
[self setImgOriginal:nil];
[self setImg1:nil];
[self setImg2:nil];
[self setImg3:nil];
[self setImg4:nil];
[self setImg5:nil];
[self setImg6:nil];
[self setImg7:nil];
[self setImg8:nil];
[self setImg9:nil];
[self setImg10:nil];
[self setImg11:nil];
[self setImg12:nil];
[self setImg13:nil];
[self setImg14:nil];
[self setImg15:nil];
[self setImg16:nil];
[self setImg17:nil];
[self setImg18:nil];
[self setImg19:nil];
[self setImg20:nil];
[self setImg21:nil];
[self setImg22:nil];
[self setImg23:nil];
[self setImg24:nil];
[self setImg25:nil];
[self setImg26:nil];
[self setImg27:nil];
[self setImg28:nil];
[self setImg29:nil];
[self setImg30:nil];
[self setImg31:nil];
[self setImg32:nil];
[self setImg33:nil];
[self setImg34:nil];
[self setImg35:nil];
[self setImg36:nil];
[self setImg37:nil];
[self setImg38:nil];
[self setImg39:nil];
[self setImg40:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(void) animateFirstTouch:(UIImageView *)image withLocation:(CGPoint)location {
image.center = location;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
image.transform = CGAffineTransformMakeScale(1.25, 1.25);
[UIView commitAnimations];
}
-(void) animateReleaseTouch:(UIImageView *)image withLocation:(CGPoint)location {
image.center = location;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
image.transform = CGAffineTransformMakeScale(1.0, 1.0);
[UIView commitAnimations];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I can't tell if its all a bit of a mess or not. As you can see i've started with only 8 "holders", these are the CGPoints that represent the center of where the jigsaw pieces should be placed.
borrrden is correct. You've created a whole bunch of CGPoint pointer variables by declaring them as properties and synthesizing them. But then in your -viewDidLoad method you're declaring CGPoint structs--not CGPoint pointers--all with the same name as the properties you synthesized.
You don't need the holderN properties at all, to judge from the code. In fact, you don't even need the holderN variables in the -viewDidLoad method. You could easily get away with something like this:
- ( void )viewDidLoad
{
holders = [ NSMutableArray arrayWithObjects:
[ NSValue valueWithCGPoint: CGPointMake(60, 644) ],
...,
[ NSValue valueWithCGPoint: CGPointMake(N, N) ],
nil ];
...
}
Hard-coding the point coordinates is poor design, though. Your best option is to put the coordinates in a plist, which you can load on demand and stuff into an array. This'll mean tweaking the coordinates is just a matter of editing the plist. No recompiling required.
You are getting the message in your comment because you declare variables with the same name as your properties in the beginning of viewDidLoad.
NSPoint is not an object so you have to either use C array: NSPoint points[SIZE] or use NSValue container: [NSValue valueWithCGPoint: point]

UIScrollVIew is not scrolling

I have a UIScrollVIew. It contains a view with different controls. I added all of them to a view and then add the view to the UIScrollView. The scroll is not activated, and the content is not centered. Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = texto.frame;
frame.size.height = texto.contentSize.height;
texto.frame = frame;
[self configureView];
[self.scrollView addSubview:self.contentView];
self.scrollView.contentSize = self.contentView.bounds.size;
self.contentView.userInteractionEnabled= NO;
self.scrollView.userInteractionEnabled=TRUE;
}
-(void)configureView{
self.texto.text = self.newsDetail.text;
NSData *imageData= [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.newsDetail.image_large]];
self.imagen.image = [UIImage imageWithData:imageData];
self.fecha.text = self.newsDetail.datePublish;
self.titulo.text = self.newsDetail.title;
}
Header code:
#interface detailNewsController : UIViewController{
UIScrollView * __weak scrollView;
UIView * __weak contentView;
}
#property (nonatomic, strong) news *newsDetail;
#property ( weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UIView * contentView;
#property (strong, nonatomic) IBOutlet UIImageView *imagen;
#property (strong, nonatomic) IBOutlet UILabel *fecha;
#property (strong, nonatomic) IBOutlet UILabel *titulo;
#property (strong, nonatomic) IBOutlet UITextView *texto;
-(void) configureView;
I see that you link your UIScrollView with a code variable. So check in interface builder if you enabled vertical scrolling (or horizontal one if you need it)

Converting UITextField to UILabel through view Controllers

I'm sure there is a simple explanation for this.
I am looking to transfer the data (not store) but transfer the text the user types into a text field and display it as a UILabel in another ViewController.
I already know how to convert text entered by the user into a label on the same viewcontroller.
I guess my problem is importing.
the .h:
#interface ViewController : UIViewController {
IBOutlet UITextField *firstPerson;
IBOutlet UITextField *secondPerson;
IBOutlet UIButton *calculateButton;
NSString *firstName;
NSString *secondName;
}
#property (nonatomic, retain) IBOutlet UITextField *firstPerson;
#property (nonatomic, retain) IBOutlet UITextField *secondPerson;
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *secondName;
#property (nonatomic, retain) IBOutlet UIButton *calculateButton;
-(IBAction)calculate;
#end
the .m:
-(IBAction)calculate {
//Linked to UIButton
//This is the first View Controller.
// firstName = firstPerson.text;
// secondName = secondPerson.text;
secondViewController = [[ShowStats alloc] init];
}
my secondview controller .m (ShowStats):
#import "ShowStats.h"
#import "ViewController.h"
- (void)viewDidLoad
{
firstName = firstPerson.text;
secondName = secondPerson.text;
[super viewDidLoad];
}
Many thanks!
EDIT
ViewController.h
#import <UIKit/UIKit.h>
#import "ShowStats.h"
#interface ViewController : UIViewController {
IBOutlet UITextField *firstPerson;
IBOutlet UITextField *secondPerson;
IBOutlet UIButton *calculateButton;
//NSString *firstName;
// NSString *secondName;
}
#property (nonatomic, retain) IBOutlet UITextField *firstPerson;
#property (nonatomic, retain) IBOutlet UITextField *secondPerson;
//#property (nonatomic, retain) NSString *firstName;
//#property (nonatomic, retain) NSString *secondName;
#property (nonatomic, retain) IBOutlet UIButton *calculateButton;
-(IBAction)calculate;
#end
ViewController.m
#import "ViewController.h"
#import "ShowStats.h"
#implementation ViewController
#synthesize firstPerson, secondPerson;
//#synthesize firstName, secondName;
#synthesize calculateButton;
ShowStats *secondViewController;
-(IBAction)calculate {
secondViewController = [[ShowStats alloc] init];
secondViewController.firstName = firstPerson.text;
}
ShowStats.h
#interface ShowStats : UIViewController{
IBOutlet UILabel *nameStats;
}
#property (nonatomic, retain) IBOutlet UILabel *nameStats;
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *secondName;
#end
ShowStats.m
- (void)viewDidLoad
{
nameStats.text = [NSString stringWithFormat:#"%#", firstName];
//secondLabel.text = self.secondName;
[super viewDidLoad];
}
Make these properties in ShowStats class
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *secondName;
and change this to this
-(IBAction)calculate {
secondViewController = [[ShowStats alloc] init];
secondViewController.firstName = firstPerson.text;
secondViewController.secondName = secondPerson.text;
}
then set these strings to UILablel in your viewDidLoad
In ShowStats.h, add the following:
#property(nonatomic, copy) NSString* firstName;
#property(nonatomic, copy) NSString* secondName;
In ShowStats.m add/update the following:
#synthesize firstName, secondName;
//...
- (id) init {
if (self = [super init]) {
self.firstName = nil;
self.secondName = nil;
//...
}
return self;
}
- (void) dealloc {
self.firstName = nil;
self.secondName = nil;
//...
[super dealloc];
}
- (void)viewDidLoad {
//or even better, do this in viewWillAppear instead
firstLabel.text = self.firstName;
secondLabel.text = self.secondName;
//...
[super viewDidLoad];
}
Finally, in ViewController.m, implement calculate like this:
-(IBAction)calculate {
//Linked to UIButton
//This is the first View Controller.
// firstName = firstPerson.text;
// secondName = secondPerson.text;
secondViewController = [[ShowStats alloc] init];
secondViewController.firstName = firstPerson.text;
secondViewController.secondName = secondPerson.text;
//display the view controller here
[secondViewController autorelease];
}
If it is a navigation then in your SecondViewController you can call:
ViewController *viewController = [self.navigationController.viewControllers objectAtIndex:0];
firstName = [[viewController firstPerson] text];
secondName = [[viewController secondPerson] text];
Or you could do the following for a Single View app:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
ViewController *viewController = [appDelegate viewController];
firstName = [[viewController firstPerson] text];
secondName = [[viewController secondPerson] text];
* EDIT *
Add two labels to your .h file (nonatomic, retain) & synthesize in .m . Initialize the labels in viewDidLoad then set them (assuming they are called label1 and label2):
[label1 setText:firstName];
[label2 setText:secondName];