I am parsing JSON using AFNetworking.
i passed 2 images and text from one view to another but i cant view the image on the UIImage view.
I can see the link of the images on the console.
here is my code (link to github)
#import "KKDetailsViewController.h"
#interface KKDetailsViewController ()
#end
#implementation KKDetailsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.text = [self.importVideoMetaData objectForKey:#"description"];
self.imageView.image = [[UIImage alloc] init];
self.imageView.image = [UIImage imageNamed:[(NSDictionary *)self.importThumbnail objectForKey:#"hqDefault"]];
NSLog(#" imported thumbnail %#", self.importThumbnail);
}
here is the .h file
#import <UIKit/UIKit.h>
#interface KKDetailsViewController : UIViewController
#property (weak, nonatomic) NSDictionary *importVideoMetaData;
#property (weak, nonatomic) NSDictionary *importThumbnail;
#property (weak, nonatomic) IBOutlet UITextView *textView;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
ok here is how i made it work:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.text = [self.importVideoMetaData objectForKey:#"description"];
NSURL *url = [[NSURL alloc] initWithString:[self.importThumbnail objectForKey:#"hqDefault"]];
[self.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"fakeThumbnail.png"]];
NSLog(#" imported thumbnail %#", url);
}
Related
Im trying to get the label and images of each item in my plist library to show up in my details tab.
Here is a pic of my tableview which i can get my plist dictionary to work. An array of images and an array or items. (ex item 0 is both the thermostat.jpg and Thermostat label.
Here is a pic of the detail view when clicking on the cell. It pulls the label from the cell and the image of the thermostat.
However when clicking on item 1 in plist (the toilet) it pulls the toilet name but still shows the thermostat image. Like so.
Here is my code for detailviewcontroller
the.h
#import <Foundation/Foundation.h>
#interface DetailViewController : UIViewController
{
IBOutlet UIScrollView *scrollView;
}
#property (nonatomic, strong) IBOutlet UILabel *itemLabel;//Name of that view
#property (nonatomic, strong) NSString *itemName;
#property (nonatomic, strong) IBOutlet UIImageView *myImageView; //Image View
#property (strong, nonatomic) IBOutlet UILabel *myTextView; //Description View
#end
and .m
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
#synthesize itemLabel;
#synthesize itemName;
#synthesize myImageView;
#synthesize myTextView;
- (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.
itemLabel.text = itemName;
//**************SCROLL VIEW*************
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320,960)];
scrollView.contentInset=UIEdgeInsetsMake(0.0,0.0,90.0,0.0);
////I know below this is the Problem, BUT im not sure what its supposed to be?////
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"repairlist" ofType:#"plist"]];
NSArray *imageArray = [picturesDictionary objectForKey:#"Thumbnail"];
myImageView.image = [UIImage imageNamed:[imageArray objectAtIndex:0]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
So all in all im trying to get the image to correctly show when clicking on that cell
Here is an image of my plist im using:
I already have this in my TableViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"ItemListCell"]) {
DetailViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
destViewController.itemName = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.myTableView indexPathForSelectedRow];
destViewController.itemName = [repairList objectAtIndex:indexPath.row];
//destViewController.textView = [descrip objectAtIndex:indexPath.row];
}
}
}
Here is your problem,
myImageView.image = [UIImage imageNamed:[imageArray objectAtIndex:0]];
You set your image always the same index which is 0. So, logically, you're always seeing the same image. So, what you need to is that you need to detect which cell was selected before you push DetailViewController and pass that info to your DetailViewController. Then, you need to use that info to show appropriate image in your DetailViewController.
First, you need to declare a property in DetailViewController.h,
#property int selectedRow;
change also this line of code
myImageView.image = [UIImage imageNamed:[imageArray objectAtIndex:0]];
to
myImageView.image = [UIImage imageNamed:[imageArray objectAtIndex:self.selectedRow]];
Then in your TableViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"YourSegueIdentifier"]) {
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.selectedRow = [dataController objectInListAtIndex:selectedRowIndex.row];
}
}
i have a TableView with some Names.
And i want to show these Names on an other ViewController, but i donĀ“t get it.
Here is my code:
AnguckenViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *DetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
AnguckenViewController *DetailKey = [self.originalsource objectAtIndex:[tableView indexPathForSelectedRow].row];
const char *cPlainKey = [DetailKey cStringUsingEncoding:NSASCIIStringEncoding];
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString *pszDetailValueString = [NSString stringWithFormat:#"%s",szSafeKey];
NSString *szDetailValue = [temp objectForKey:pszDetailValueString];
[DetailViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:DetailViewController animated:YES completion:^(void){}];
}
DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController {
IBOutlet UIButton *BackButton;
NSString *_pszKey;
NSString *_pszValue;
}
#property (nonatomic, retain) NSString *pszKey;
#property (nonatomic, retain) NSString *pszValue;
#property (retain, nonatomic) IBOutlet UILabel *keyLabel;
#property (retain, nonatomic) IBOutlet UILabel *valueLabel;
#end
DetailViewController.m
#implementation DetailViewController
#synthesize pszKey;
#synthesize pszValue;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_keyLabel.text = _pszKey;
}
You need to set the pszKey property of the view controller, e.g. the DetailViewController, before you display it modally.
DetailViewController.pszKey = #"The value";
[DetailViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:DetailViewController animated:YES completion:^(void){}];
And also in the viewDidLoad, it is good to use the getter of the properties instead of accessing the instance variables directly:
- (void)viewDidLoad
{
[super viewDidLoad];
self.keyLabel.text = self.pszKey;
}
So finally it works,
i changed the line:
UIViewController *DetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
to:
DetailViewController *DetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
Thanks
I am currently dynamically adding a UIImage to my UIScrollView object and everything is working as intended. I need to add extra functionality now (a UIActivityIndicator in the center of the image when it is loading from the internet, and a label underneath) so I thought why not create a custom View which has all of these laid out as I need them to be, and then just load it into the scrollView. I have encountered a problem though, when I add it to the scrollView, nothing shows up. Here is what I have:
NewsViewController.m:
imageScrollView.contentSize = CGSizeMake(320*numberOfPages, 303);
pageControl.numberOfPages = numberOfPages;
dispatch_queue_t imageDownload = dispatch_queue_create("imageDownload", NULL);
__block NSData *temp;
CustomImageView *customImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 303)];
[imageScrollView addSubview:customImageView];
[[customImageView activityIndicator] startAnimating];
dispatch_async(imageDownload, ^{
temp = [[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.wlfarm.org/wp-content/uploads/2012/05/farm.jpg"]]retain];
dispatch_async(dispatch_get_main_queue(), ^{
customImageView.imageView.image = [[UIImage alloc] initWithData:temp];
[[customImageView activityIndicator] stopAnimating];
[customImageView setNeedsDisplay];
customImageView.caption.text = #"HAHAHAHAHHAHA";
[imageScrollView setNeedsDisplay];
[temp release];
});
});
dispatch_release(imageDownload);
CustomImageView.h:
#import <UIKit/UIKit.h>
#interface CustomImageView : UIView
{
IBOutlet UIImageView *imageView;
IBOutlet UILabel *caption;
IBOutlet UIActivityIndicatorView *activityIndicator;
}
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UILabel *caption;
#property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
#end
CustomImageView.m
#import "CustomImageView.h"
#interface CustomImageView ()
#end
#implementation CustomImageView
#synthesize caption, imageView, activityIndicator;
- (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.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I am including a screenshot of my XIB file, and the program running on the simulator. The first picture shows nothing in the scrollView(the attempt made by using my custom class), and the second page of the scroll view is the attempt made by adding a UIImageView to the UIScrollView(which worked). Can anyone point out what I am doing wrong? Am I not allowed to load a custom view into a UIScrollView? Thanks for your help!
IB Screenshot - http://i.stack.imgur.com/gz9UL.png
iOS Simulator No Image with CustomView Screenshot - http://i.stack.imgur.com/zhswq.png
iOS Simulator Image with UIImageView Screenshot - http://i.stack.imgur.com/97vmU.png
It looks as though CustomImageView is a subclass on UIViewController, not UIImageView or UIView. You can't add a UIViewController as a subview like that. change it to subclass UIView and it should work.
To load a UIView from a .nib, you need to declare your IBOutlet properties as you would for a UIViewController. Define the correct custom class in IB and connect everything up, including the base view. Then override the following method inside your custom view class.
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
//
[[NSBundle mainBundle] loadNibNamed:#"<NIB NAME HERE>" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}
It seems you have cut and pasted methods from a UIViewController subclass into you UIView subclass. Start by deleting all the methods you've pasted in between #synthesize and #end . A UIView subclass requires different methods to to load from a .nib, so you need to override the method shown above instead and use the line of code
[[NSBundle mainBundle] loadNibNamed:#"<NIB NAME HERE>" owner:self options:nil];
to load the nib.
Further to your comment about connecting the base view, here is what I mean:
After creating your custom class and nib file, open the nib and highlight "Files Owner" on the left, then on the top right hand side, select the icon 3rd from the left, looks like an ID card or something. In the "Class" box, add the name of your custom class.
In your customClass, add an IBOutlet UIView property called baseView, and add a UIView that covers the view in IB at the root level, connect these two. Then add everything else on top of that
you code would now look something like this:
CustomImageView.h
#import <UIKit/UIKit.h>
#interface CustomImageView : UIView
{
IBOutlet UIView *baseView
IBOutlet UIImageView *imageView;
IBOutlet UILabel *caption;
IBOutlet UIActivityIndicatorView *activityIndicator;
}
#property (nonatomic, retain) IBOutlet UIView *baseView;
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UILabel *caption;
#property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
#end
CustomImageView.m
#import "CustomImageView.h"
#interface CustomImageView ()
#end
#implementation CustomImageView
#synthesize baseView, caption, imageView, activityIndicator;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
//
[[NSBundle mainBundle] loadNibNamed:#"<NIB NAME HERE>" owner:self options:nil];
[self addSubview:self.baseView];
}
return self;
}
#end
Provided you connect it all up in IB it should work fine, just remember to add the baseView
As mentioned earlier initWithNibName belongs to controllers.
As I think, you're going the wrong way.
you have a ViewController and want to add a customView, to load dynamically a image from URLData.
You have 2 Options:
Option 1: Do everything in IB:
In Interfacebuilder edit/(or add a new one) the ViewController's XIB File and add the views that you like. The file's owner is a UIViewController Class/Subclass. Do everything like you did before, except doing it in the view controller. In your App delegate initWithNibName your ViewController like so
self.viewController = [[testViewController alloc] initWithNibName:#"testViewController" bundle:nil];
If you want to, initialize your own view controller, which you like to push and push it in the stack.
What you're doing wrong: You are initializing a customImageView with a Frame, but the nib doesn't get loaded automatically. The Properties are there, but the nib isn't.
If you really want a stable thing choose
Option 2: Do everything programatically (It is easier, more understanding and lightweight):
From your implementation we do the following:
NewsViewController.m
Do like you did before!!!
CustomImageView.h:
#import <UIKit/UIKit.h>
#interface CustomImageView : UIView
{
UIImageView *imageView;
UILabel *caption;
UIActivityIndicatorView *activityIndicator;
}
#property (nonatomic, retain) UIImageView *imageView;
#property (nonatomic, retain) UILabel *caption;
#property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
#end
CustomImageView.m:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
caption = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, 320, 50)];
[caption setBackgroundColor:[UIColor greenColor]];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
[imageView setBackgroundColor:[UIColor blueColor]];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicator.frame = CGRectMake(320/2-25, 460/2-25, 50, 50);
[self.activityIndicator startAnimating];
[self addSubview:self.caption];
[self addSubview:self.imageView];
[self addSubview:self.activityIndicator];
}
return self;
}
Then do everything you did before.
That would do a better job
I am doing some exercises with delegate, but now I have a problem with a UIView. This is my storyboard
I want to change the color of the UIView with 3 UISliders. The range of UISliders is from 0 to 255.
And this is my code:
ColorField is the UIView custom class
ColorField.h
#import <UIKit/UIKit.h>
#protocol ColorFieldDelegate <NSObject>
-(NSArray *)giveMeColors;
#end
#interface ColorField : UIView
#property (nonatomic , weak) IBOutlet id<ColorFieldDelegate> delegate;
#end
ColorField.m
#import "ColorField.h"
#implementation ColorField
#synthesize delegate = _delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
NSArray *arrayOfColors = [self.delegate giveMeColors];
int red = [[arrayOfColors objectAtIndex:0] intValue];
int green = [[arrayOfColors objectAtIndex:1] intValue];
int blue = [[arrayOfColors objectAtIndex:2] intValue];
NSLog(#"Red --> %d" ,red);
NSLog(#"Green --> %d" ,green);
NSLog(#"Blue --> %d \n\n" ,blue);
self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
#end
ColorViewController.h
#import <UIKit/UIKit.h>
#import "ColorField.h"
#interface ColorViewController : UIViewController <ColorFieldDelegate>
#property (nonatomic) IBOutlet ColorField *colorField;
#property (weak, nonatomic) IBOutlet UISlider *redSlider;
#property (weak, nonatomic) IBOutlet UISlider *greenSlider;
#property (weak, nonatomic) IBOutlet UISlider *blueSlider;
#end
ColorViewController.m
#import "ColorViewController.h"
#interface ColorViewController ()
#property (nonatomic) double redQuantity;
#property (nonatomic) double greenQuantity;
#property (nonatomic) double blueQuantity;
#end
#implementation ColorViewController
#synthesize colorField = _colorField;
#synthesize redSlider;
#synthesize greenSlider;
#synthesize blueSlider;
#synthesize redQuantity;
#synthesize blueQuantity;
#synthesize greenQuantity;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.colorField setDelegate:self];
[self.colorField setNeedsDisplay];
self.redQuantity = 125.0;
self.blueQuantity = 125.0;
self.greenQuantity = 125.0;
[self.colorField setNeedsDisplay];
// Do any additional setup after loading the view, typically from a nib.
}
-(ColorField *)colorField
{
if (_colorField == nil) {
_colorField = [[ColorField alloc] init];
}
return _colorField;
}
- (void)viewDidUnload
{
[self setColorField:nil];
[self setRedSlider:nil];
[self setGreenSlider:nil];
[self setBlueSlider:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(IBAction)changeRedQuantity:(UISlider *)sender
{
//methods of UISliders
self.redQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(IBAction)changeBlueQuantity:(UISlider *)sender
{
self.blueQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(IBAction)changeGreenQuantity:(UISlider *)sender
{
self.greenQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(NSArray *)giveMeColors
{
NSNumber *redNumber = [NSNumber numberWithDouble:self.redQuantity];
NSNumber *greenNumber = [NSNumber numberWithDouble:self.greenQuantity];
NSNumber *blueNumber = [NSNumber numberWithDouble:self.blueQuantity];
NSArray *array = [[NSArray alloc] initWithObjects:redNumber, greenNumber,blueNumber, nil];
return array;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
BUT...this is the result: It show me only RED, GREEN and BLUE colors without gradation, for example: RGB (255,0,75) is THIS
and not THIS:
I don't know with it can't show me gradation...
Thanks!!
Marco Manzoni
You have to divide your slidervalues by 255.0.
[UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
colorWithRed only accepts values 0.0-1.0
I used the following code to Webview navigation.
The following code works well some URLS, but it doesnt work some URL.. I dont know what i did mistake in my code...
.h
#import <UIKit/UIKit.h>
#interface WebviewViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView * webview;
IBOutlet UIToolbar * tBar;
IBOutlet UIBarButtonItem * backbtn;
IBOutlet UIBarButtonItem * forwardbtn;
}
#property (nonatomic, retain) IBOutlet UIWebView * webview;
#property (nonatomic, retain) IBOutlet UIBarButtonItem * backbtn;
#property (nonatomic, retain) IBOutlet UIBarButtonItem * forwardbtn;
#property (nonatomic, retain) UIToolbar * tBar;
- (IBAction)goBackbttn:(id)sender;
- (IBAction)goForwardbttn:(id)sender;
#end
.m
#import "WebviewViewController.h"
#implementation WebviewViewController
#synthesize webview;
#synthesize tBar, backbtn, forwardbtn;
- (void)viewDidLoad {
[super viewDidLoad];
[backbtn setEnabled : FALSE];
[forwardbtn setEnabled : FALSE];
NSString * urlAddress = # "http://www.exampl.com";
NSURL * url = [NSURL URLWithString:urlAddress];
NSURLRequest * requestObj = [NSURLRequest requestWithURL:url];
webview.scalesPageToFit = YES;
webview.autoresizesSubviews = YES;
[webview loadRequest : requestObj];
}
- (IBAction)goBackbttn:(id)sender
{
[forwardbtn setEnabled : TRUE];
}
- (IBAction)goForwardbttn:(id)sender {
[webview goForward];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[backbtn setEnabled :[webView canGoBack]];
[forwardbtn setEnabled :[webView canGoForward]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
Shouldn't this
- (IBAction)goBackbttn:(id)sender
{
[forwardbtn setEnabled : TRUE];
}
be this:
- (IBAction)goBackbttn:(id)sender
{
[webview goBack];
}
?