Converting UITextField to UILabel through view Controllers - objective-c

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];

Related

table view no data showing in the table cell

I have face problem in that code when i have run that code then no data show in tableview on it's table cell. there is no error in console but data not show. please Guide me what's the mistake i have do that code.
InvoiceCell.h
#import <UIKit/UIKit.h>
#interface InvoiceCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *ItemImage;
#property (strong, nonatomic) IBOutlet UILabel *ItemName;
#property (strong, nonatomic) IBOutlet UILabel *ItemType;
#property (strong, nonatomic) IBOutlet UILabel *date;
#property (strong, nonatomic) IBOutlet UILabel *Amount;
#property (strong, nonatomic) IBOutlet UILabel *status;
#end
InvoiceCell.m
#import "InvoiceCell.h"
#implementation InvoiceCell
#synthesize ItemImage,ItemName,ItemType,Amount,status;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
InvoiceViewController.h
#import <UIKit/UIKit.h>
#interface InvoiceViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic,strong) NSArray *DataImages;
#property (nonatomic, strong) NSArray *DataName;
#property (nonatomic, strong) NSArray *Datatype;
#property (nonatomic, strong) NSArray *DataAmount;
#property (nonatomic, strong) NSArray *DataStatus;
#property (nonatomic, strong) NSArray *Datadate;
#end
InvoiceViewController.m
#import "InvoiceViewController.h"
#import "InvoiceCell.h"
#interface InvoiceViewController ()
#end
#implementation InvoiceViewController
#synthesize DataImages,DataName,Datatype,DataAmount,DataStatus,Datadate;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
DataImages = #[#"img1.jpg", #"img2.jpg", #"img3.jpg", #"img4.jpg", #"img5.jpg"];
DataName = #[#"Suit", #"Shirt", #"paint", #"Suit", #"Suit"];
Datatype = #[#"Brown", #"Black", #"Blue", #"Black", #"Blue"];
DataAmount = #[#"$500", #"$400",#"$500", #"$400",#"$300"];
DataStatus = #[#"Paid", #"Due", #"Paid", #"Due", #"Paid"];
Datadate = #[#"8/5/2014", #"10/6/2014",#"8/5/2014", #"10/6/2014",#"8/5/2014"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return DataName.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"InvoiceCell";
InvoiceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
cell = [[InvoiceCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
long row = [indexPath row];
NSLog(#"%#", cell.ItemName.text = DataName[row]);
cell.ItemName.text = DataName[row];
cell.ItemType.text = Datatype[row];
cell.ItemImage.image = [UIImage imageNamed:DataImages[row]];
cell.date.text = Datadate[row];
cell.Amount.text = DataAmount[row];
cell.status.text = DataStatus[row];
return cell;
}
In the Debug Window and console Data show but not show data on simulator screen in the tableview cells
Thank you.
You may have a tableView in your StoryBoard, but you have no connection to it in your code. Add this to your InvoiceViewController.h
#property IBOutlet UITableView *tableView;
and hook it up to your TableView in Interface Builder.
Then you can set the DataSource and Delegate in your InvoiceViewController.m -[viewDidLoad] method
_tableView.delegate = self;
_tableView.dataSource = self;
Now the TableView will know to call the delegate methods you implemented and you should see your data in the table.

Cant't show a integer in a label

I'm trying to make a very simple iOS app where an integer is created within a MutableArray.
The integer must present itself in an label in a Custom ViewController. Here is my code:
#interface Photo : NSObject
#property (nonatomic) NSString *name;
#property (nonatomic) NSString *filename;
#property (nonatomic) NSString *naam;
#property (nonatomic) int leeftijd;
#property (nonatomic) NSString *herkomst;
#property (nonatomic) NSString *club;
#end
My Custom TableViewController with an mutableArray.
#interface PhotosTableViewController () {
NSMutableArray *photos;
}
#end
#implementation PhotosTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Soccer Players";
// Create an array
photos = [[NSMutableArray alloc]init];
Photo *pic = [[Photo alloc]init];
pic.name = #"Ronaldo";
pic.filename = #"ronaldo";
pic.naam = #"Christano Ronaldo";
pic.leeftijd = 29;
pic.herkomst = #"Portugal";
pic.club = #"Real Madrid";
[photos addObject:pic];
And at last my Custom ViewController
#interface DisplayViewController ()
#property (weak, nonatomic) IBOutlet UIImageView *currentImage;
#property (weak, nonatomic) IBOutlet UILabel *nameLabel;
#property (weak, nonatomic) IBOutlet UILabel *ageLabel;
#property (weak, nonatomic) IBOutlet UILabel *countryLabel;
#property (weak, nonatomic) IBOutlet UILabel *clubLabel;
#end
#implementation DisplayViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:self.currentPhoto.filename];
[self.currentImage setImage:image];
self.nameLabel.text = [self.currentPhoto naam];
self.ageLabel.text = [self.currentPhoto leeftijd];
self.countryLabel.text = [self.currentPhoto herkomst];
self.clubLabel.text = [self.currentPhoto club];
}
Not sure what i'm doing wrong. I gave the integer a Property , gave it a amount en declared it in a label. Must be something very stupid, but can't see the solution on the web.
Thanks for your help!
You should 'convert' int to string:
self.ageLabel.text = [NSString stringWithFormat:#"%d", [self.currentPhoto leeftijd]];
Integer is not auto converted to NSString. Change
self.ageLabel.text = [self.currentPhoto leeftijd];
to
self.ageLabel.text = [[self.currentPhoto leeftijd] description];
or to
self.ageLabel.text = [NSString stringWithFormat:#"%i", [self.currentPhoto leeftijd]];
Lable only show NSString so you convert your int to NSString..
self.ageLabel.text = [NSString stringWithFormat:#"%d", [self.currentPhoto leeftijd]];

Not receiving reply from notification

I am trying to get a reply from a notification and pass the string to a label.
But the label (nResponse) does not change after I give the reply, why isn't the label updating? Is there a problem with the "activationType" method?
AppDelegate.h
#property (weak) IBOutlet NSTextField *nTitle;
#property (weak) IBOutlet NSTextField *nMessage;
-(IBAction)showNotification:(id)sender;
#property (weak) IBOutlet NSTextField *nResponse;
#property (assign) IBOutlet NSWindow *window;
#property BOOL hasReplyButton NS_AVAILABLE(10_9, NA);
#property (copy) NSString *responsePlaceholder NS_AVAILABLE(10_9, NA);
#property (readonly) NSAttributedString *response NS_AVAILABLE(10_9, NA);
#property (copy) NSImage *contentImage NS_AVAILABLE(10_9, NA);
#end
AppDelegate.m
#synthesize nTitle;
#synthesize nMessage;
#synthesize nResponse;
#synthesize window;
- (IBAction)showNotification:(id)sender{
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = [nTitle stringValue];
notification.informativeText = [nMessage stringValue];
notification.soundName = NSUserNotificationDefaultSoundName;
notification.responsePlaceholder = #"Reply";
notification.hasReplyButton = true;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification;
{
if (notification.activationType == NSUserNotificationActivationTypeReplied){
NSString* userResponse = notification.response.string;
[nResponse setStringValue:userResponse]; //set label to response
NSLog(#"%#", userResponse);
}
return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}
#end
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = self;
You should also make sure your Notice class implements the NSUserNotificationCenterDelegate protocol.

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;
}

Image is not loading on UIImageView

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)