Passing data between two ViewControllers Problem - iphone

Hi I have two viewControllers.
NewsViewController
#import "SingleViewController.h"
#interface NewsViewController : UIViewController {
}
- (NSString *)getFirstImage:(NSString *)htmlString;
#end
and the implementation file:
#import "SingleViewController.h"
SingleViewController *singleViewController;
#interface NewsPadViewController () <UIActionSheetDelegate>
- (void)loadSubscriptions;
#property (nonatomic, assign) BOOL wrap;
#property (nonatomic, retain) NSMutableArray *items;
#end
#implementation NewsPadViewController
....CODE....
- (void)carouselCurrentItemTapped{
//[self addSubview:articolo];
MWFeedItem *item =[reader.feedItems objectAtIndex:[carousel currentItemIndex]];
NSLog(#"Current Item tappped, index: %d", [carousel currentItemIndex]);
singleViewController.prova.text=#"CIAO";
singleViewController.ciao=#"sample";
[singleViewController.web loadHTMLString:item.summary baseURL:nil];
NSLog(#"conternutio:");
NSLog(singleViewController.ciao);
}
SingleViewController.h
#import <UIKit/UIKit.h>
#interface SingleViewController : UIViewController{
#public
UIWebView *web;
UILabel *prova;
NSString *ciao;
}
#property (nonatomic,retain) IBOutlet UIWebView *web;
#property (nonatomic,retain) IBOutlet UILabel *prova;
#property (nonatomic,retain) IBOutlet NSString *ciao;
#end
and SingleViewController.m
#import "SingleViewController.h"
#implementation SingleViewController
#synthesize web,prova,ciao;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[prova setText:ciao];
[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 YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Why I can't access from NewsPadViewController at object in SingleViewController? Where is my error? thanks
UPADATE
- (void)carouselCurrentItemTapped{
//[self addSubview:articolo];
MWFeedItem *item =[reader.feedItems objectAtIndex:[carousel currentItemIndex]];
NSLog(#"Current Item tappped, index: %d", [carousel currentItemIndex]);
SingleViewController *singleViewController = [[SingleViewController alloc] initWithNibName:#"SingleViewController" bundle:[NSBundle mainBundle]];
singleViewController.prova.text=#"CIAO";
singleViewController.ciao=#"sample";
[singleViewController.web loadHTMLString:item.summary baseURL:nil];
NSLog(#"conternutio:");
NSLog(singleViewController.ciao);
[singleViewController setOpz:#"sample"];
}

I don't see any instantiation of SingleViewController. You have a pointer to it, but I don't see anywhere in your code example where you actually create it.
Somewhere you need something like:
if (!singleViewController) {
singleViewController = [[SingleViewController alloc] init];
}
or
SingleViewController *singleViewController = [[SingleViewController alloc] initWithNibName:#"SingleView" bundle:nil];
or from somewhere else you need to send a message or set an instance variable in NewsViewController that points to an existing instance of SingleViewController.

Related

Why doesn't Label receive string

I have two views and two classes. On the first view, controlled by the first class I have a textfield and a button. On the second view controlled by the second class I have two labels. I tried to make one label receive the value I put on the text field, and the other label this value divided by 100. But on both cases I get 0.00, I don't know why!
firstclass.h
#import <UIKit/UIKit.h>
#interface NotasViewController : UIViewController{
}
-(IBAction)calcular:(id)sender;
-(IBAction)clicarFora:(id)sender;
-(IBAction)recuarTeclado:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *inserirTF;
#end
fisrtclass.m
#import "NotasViewController.h"
#import "resultadoViewController.h"
#interface NotasViewController ()
#end
#implementation NotasViewController
#synthesize inserirTF;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)calcular:(id)sender{
float valor = [inserirTF.text floatValue];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
resultadoViewController *second = [storyboard instantiateViewControllerWithIdentifier:#"resultado"];
second.resultadoFloatValor = valor;
NSLog(#"%.2f", second.resultadoFloatValor);
}
-(IBAction)recuarTeclado:(id)sender{
[sender resignFirstResponder];
}
-(IBAction)clicarFora:(id)sender{
[inserirTF resignFirstResponder];
}
#end
secondclass.h
#import <UIKit/UIKit.h>
#interface resultadoViewController : UIViewController{
float resultadoFloatValor;
}
#property float resultadoFloatValor;
#property (strong, nonatomic) IBOutlet UILabel *resultadoLabelValor;
#property (strong, nonatomic) IBOutlet UILabel *resultadoLabelQtd100;
#end
secondclass.m
#import "resultadoViewController.h"
#interface resultadoViewController ()
#end
#implementation resultadoViewController
#synthesize resultadoFloatValor, resultadoLabelValor, resultadoLabelQtd100;
- (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.
NSString *resultadoStringValor = [[NSString alloc] initWithFormat:#"%.2f", resultadoFloatValor];
NSLog(#"%.2f", resultadoFloatValor);
NSLog(#"%#", resultadoStringValor);
resultadoLabelValor.text = resultadoStringValor;
int resultadoIntResto100 = resultadoFloatValor/100;
if (resultadoIntResto100 > 0) {
NSString *resultadoStringQtd100 = [[NSString alloc] initWithFormat:#"%i", resultadoIntResto100];
resultadoLabelQtd100.text = resultadoStringQtd100;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You'll see 3 NSLogs on my code. For testing, I put 54 on my textfield. The Output was like this:
2013-01-12 21:15:27.020 Divisorreal[21843:c07] 54.00
2013-01-12 21:15:27.023 Divisorreal[21843:c07] 0.00
2013-01-12 21:15:27.023 Divisorreal[21843:c07] 0.00
PS: All my Outlets are correctly connected to the UILabels.
Best guess:
It looks like you are setting resultadoFloatValor after viewDidLoad is called.
Implement prepareForSegue: and set the value there.
Make sure you set the UITextView's delegate to send those actions to your controller.
Try putting a breakpoint in
-(IBAction)calcular:(id)sender
method to ensure its actually being called

[PointInfoController endAppearanceTransition]: message sent to deallocated instance 0x74d5e90

I need a little help here, I've loaded a view by modal and basically I'm now trying to dismiss this view now but it keep crashing after the view goes.
It says * -[PointInfoController endAppearanceTransition]: message sent to deallocated instance 0x74d5e90
Below is the .H file:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#class PointInfoController;
#protocol PointInfoDelegate
- (void)PointInfoDidFinish:(PointInfoController*)PointInfoController;
#end
#interface PointInfoController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UIScrollView *scrollView;
IBOutlet UIImageView *thumbnailView;
IBOutlet UIButton *Dismiss;
id<PointInfoDelegate> delegate;
}
#property (retain) id<PointInfoDelegate> delegate;
#property (retain, nonatomic) IBOutlet UITextView *description;
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
#property (retain, nonatomic) IBOutlet UIImageView *thumbnailView;
- (IBAction)DismissView:(id)sender;
#end
Below is the .M file:
#import "PointInfoController.h"
#import "LockOnLocation.h"
#implementation UIScrollView (AutoContentSize)
- (void) setAutosizeContent:(BOOL)autosizeContent {
if (autosizeContent) {
CGFloat contentWidth =
self.frame.size.width == self.superview.frame.size.width ?
self.superview.frame.size.width :
self.frame.size.width + 10;
CGFloat contentHeight =
self.frame.size.height == self.superview.frame.size.height ?
self.superview.frame.size.height :
self.frame.size.height + 164;
self.contentSize = CGSizeMake(contentWidth, contentHeight);
}
}
#end
#implementation PointInfoController
#synthesize description;
#synthesize scrollView;
#synthesize thumbnailView;
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
/*
// 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
{
self.scrollView.delegate = self;
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setDescription:nil];
[scrollView release];
scrollView = nil;
[self setScrollView:nil];
[self setThumbnailView:nil];
[thumbnailView release];
thumbnailView = nil;
[Dismiss release];
Dismiss = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)DismissView:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
[self release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[description release];
[scrollView release];
[thumbnailView release];
[Dismiss release];
[super dealloc];
}
#end
I'm new to all this and I spent quite a few days on the same problem. Blamed everything but myself! I discovered I was inadvertently release(ing) the instance referred to. In my case I had put the instance into a SSLConnectionRef and CFRelease'd it. I'm not sure why you are having the issue looking at your code. In any event, I thought your compiler would complain about your [instance release] methods, since these are no longer allowed. You are having the problem because both you and the system are releasing the instance. You need to get rid of your release and all will be well.

Passing object using story board iOS5

I am trying to pass an object to a static grouped table view that I have created in story board.
Here is the code that I am using in my first view to push the second view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row Selected");
CustomerDetailTableViewController *detailView = [[self storyboard] instantiateViewControllerWithIdentifier:#"DetailsView"];
detailView.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
NSLog(#"%#",detailView.customer.firstName);
[self.navigationController pushViewController:detailView animated:YES];
}
The NSlog for the firstName is correct but when the detail view is pushed the cells in the detailView are null. I'm probably just missing something dumb but a fresh set of eyes would be much appreciated.
Here is the code for the detailView controller:
CustomerDetailTableViewController.h
#class Customer;
#import <UIKit/UIKit.h>
#interface CustomerDetailTableViewController : UITableViewController{
Customer *customer;
UILabel *fullName;
UILabel *address;
UILabel *homePhone;
UILabel *cellPhone;
UILabel *email;
}
#property (nonatomic, strong) IBOutlet UILabel *fullName;
#property (nonatomic, strong) IBOutlet UILabel *address;
#property (nonatomic, strong) IBOutlet UILabel *homePhone;
#property (nonatomic, strong) IBOutlet UILabel *cellPhone;
#property (nonatomic, strong) IBOutlet UILabel *email;
#property (nonatomic, strong) Customer *customer;
#end
CustomerDetailTableViewController.m
#import "CustomerDetailTableViewController.h"
#import "Customer.h"
#implementation CustomerDetailTableViewController
#synthesize fullName, address, homePhone, cellPhone, email, customer;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
fullName = [NSString stringWithFormat:#"%# %#", customer.firstName, customer.lastName];
address = [NSString stringWithFormat: #"%#/n%#, %# %#", customer.address, customer.city, customer.state, customer.zipCode];
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
So, you have created the two views (the first TableView and the CustomerDetailTableViewController) with storyboards?
In that case, you have to click on the connection line between the two views into the storyboard and set the "Identifier" field, into the section "Storyboard Segue", to something like "setCustomer". Here's a screenshot:
Here a little screenshot:
After that, you can comment the method tableView:didSelectRowAtIndexPath: on the first TableView, and replace with this method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"setCustomer"]) {
CustomerDetailTableViewController *customerDetailVC = (CustomerDetailTableViewController *)[segue destinationViewController];
customerDetailVC.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
}
}
Remember to include
#include "CustomerDetailTableViewController.h"
at the top of implementation file.
I hope this help!

Trouble using a UIButton to transition UIViews

I am a very inexperienced developer and have run into a problem that should be easy however after hours of reading over Apple's Developer guide and many different questions on this site I am still stumped.
All I am trying to do is transition from my root view controller to my next view controller via a UIButton.
Here is appdelegate.h:
#import <UIKit/UIKit.h>
#class MainViewController;
#interface MDAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
Important stuff from Appdelegate.m:
#import "MDAppDelegate.h"
#import "MainViewController.h"
#implementation MDAppDelegate
#synthesize window, navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
MainViewController.h:
#import <UIKit/UIKit.h>
#interface MainViewController : UIViewController {
UIButton *cityButton;
}
#property (nonatomic, retain) IBOutlet UIButton *cityButton;
- (IBAction)chooseCityAction:(id)sender;
#end
And finally, MainViewController.m:
#import "MainViewController.h"
#import "DailyDealViewController.h"
#implementation MainViewController
#synthesize cityButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)dealloc
{
[cityButton release];
[super dealloc];
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = #"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
}
- (void)viewDidUnload
{
[self setCityButton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)chooseCityAction:(id)sender {
DailyDealViewController *dailyDealViewController = [[DailyDealViewController alloc]initWithNibName:#"DailyDealViewController" bundle:nil];
[dailyDealViewController release];
[self.navigationController pushViewController:dailyDealViewController animated:YES];
}
#end
Here is the error:
2011-10-03 21:56:14.258 MD[4235:207] *** Terminating app due to uncaught exception
'NSUnknownKeyException', reason: '[<UIViewController 0x4b434c0>
setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
cityButton.'
I would love to know what I missing.
You have released dailyDealViewController before pushing it. Move the release statement to after the pushViewController statement.

Multi-View Application Problem

I want to make multi view application. My goal is when program launch to load first view controller and then when press the button to load new tab bar controller and dismiss first controller. I try myself and I do firt step, but when tab bar controller is loaded it's appear only small tab without any tabs and old controller doesn't disappear.
This is what I've done:
SwitchAppelegate
-- Header file --
#import <UIKit/UIKit.h>
#class SwitchClass;
#interface SwitchAppDelegate : NSObject <UIApplicationDelegate> {
SwitchClass *switchClass;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet SwitchClass *switchClass;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
-- Implementation file --
#import "SwitchAppDelegate.h"
#implementation SwitchAppDelegate
#synthesize window=_window;
#synthesize managedObjectContext=__managedObjectContext;
#synthesize managedObjectModel=__managedObjectModel;
#synthesize persistentStoreCoordinator=__persistentStoreCoordinator;
#synthesize switchClass;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window addSubview:[switchClass view]];
[self.window makeKeyAndVisible];
return YES;
}
SwitchClass
-- Header file --
#import <UIKit/UIKit.h>
#class BlueClass;
#interface SwitchClass : UIViewController {
BlueClass *blueClass;
}
#property (nonatomic, retain) IBOutlet BlueClass *blueClass;
#end
-- Implementation file --
#import "SwitchClass.h"
#import "BlueClass.h"
#implementation SwitchClass
#synthesize blueClass;
-(void) viewDidLoad {
BlueClass *blue = [[BlueClass alloc] initWithNibName:#"BlueClass" bundle:nil];
self.blueClass = blue;
[self.view insertSubview:blue.view atIndex:0];
[blue release];
[super viewDidLoad];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[blueClass release];
[super dealloc];
}
BlueClass
-- Header file --
#class RedClass;
#interface BlueClass : UIViewController {
RedClass *redClass;
}
#property (nonatomic, retain) IBOutlet RedClass *redClass;
-(IBAction) switch: (id) sender;
#end
-- Implementation file --
#import "BlueClass.h"
#import "RedClass.h"
#implementation BlueClass
#synthesize redClass;
-(IBAction) switch: (id) sender {
RedClass *blue = [[RedClass alloc] initWithNibName:#"RedClass" bundle:nil];
self.redClass = blue;
// self.window.rootViewController = self.tabBarController;
[self.view addSubview:blue.view];
[blue release];
[super viewDidLoad];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[redClass release];
[super dealloc];
}
RedClass
-- Header file --
#import <UIKit/UIKit.h>
#interface RedClass : UITabBarController {
}
#end
-- Implementation file --
#import "RedClass.h"
#implementation RedClass
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
First thing is that in the appDelegate you are adding the view of SwitchClass. But SwitchClass is a UIViewController class and not UIView. so instead of that you should add your SwitchClass as a rootController like this:
self.window.rootViewController = self.switchClass;
If I were you I would just use a tab bar template provided by XCode. It will do it for you automatically.