Cant set row selected for RSS app in iphone - iphone

OK, i'm making an app that pulls down rss feeds, the initial table view is the headlines, the second view shows the detail of that headline the user selected.
This is the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[self appDelegate] setCurrentlySelectedBlogItem:[[[self rssParser]rssItems]objectAtIndex:indexPath.row]];
NewsDetailViewController *newsDetail = [[NewsDetailViewController alloc] initWithNibName:#"NewsDetailViewController" bundle:nil];
//NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
// ...
// Pass the selected object to the new view controller.
newsDetail.titleTextView.text = [[[self appDelegate] currentlySelectedBlogItem]title];
newsDetail.descriptionTextView.text = [[[self appDelegate] currentlySelectedBlogItem]description];
[self.navigationController pushViewController:newsDetail animated:YES];
[newsDetail release];
}
setCurrentlySelectedBlogItem is a pointer to blogrss class which is reference in the AppDelegate. When i'm clicking on the row of the headline and its taking me to the next view, the content is empty. In the last two lines above i'm attempting to set the UILabel's as the current headline's details...
Anyone hazard a guess to why this is happening?
Also this is my AppDelegate.h file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#class RssFunViewController;
#class NewsDetailViewController;
#class RootViewController;
#class BlogRss;
#class BlogRssParser;
#interface ExampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
RootViewController *viewController;
NewsDetailViewController * newsDetailController;
BlogRss * currentlySelectedBlogItem;
#private
NSManagedObjectContext *managedObjectContext_;
NSManagedObjectModel *managedObjectModel_;
NSPersistentStoreCoordinator *persistentStoreCoordinator_;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *viewController;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet NewsDetailViewController * newsDetailController;
#property (readwrite,retain) BlogRss * currentlySelectedBlogItem;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSString *)applicationDocumentsDirectory;
- (void)saveContext;
#end

The UILabels in the view controller are probably still nil at that point and not accessible. Instead of setting the controls directly, create two NSString properties in the NewsDetailViewController and set those before you push it. Then, in the NewsDetailViewController, set the labels to the property values in the viewDidLoad method (at which point the labels should be instantiated and accessible).

Related

Calling a function in viewcontroller from appdelegate

I want to call a function in a viewController from my appDelegate but with the following code, it doesn't get called. What am I doing wrong?
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "DetailsToTransfer.h"
#class AccountDetailTransferViewController;
#interface AccountDetailTransferAppDelegate : NSObject <UIApplicationDelegate> {
DetailsToTransfer *objDetailsToTransfer;
}
#property (nonatomic, retain) DetailsToTransfer *objDetailsToTransfer;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet AccountDetailTransferViewController *viewController;
-(void)sendToTransferScreen:(NSArray *)detailsArray;
#end
AppDelegate.m:
....
-(void)sendToTransferScreen:(NSArray *)detailsArray {
[objDetailsToTransfer setLabels:detailsArray];
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[self.window addSubview:objDetailsToTransfer.view];
}
DetailsToTransfer.h:
#import <UIKit/UIKit.h>
#interface DetailsToTransfer : UIViewController {
NSArray *accountDetailsArray;
UILabel *nameLabel;
UILabel *accountLabel;
IBOutlet UIButton *btnTransfer;
IBOutlet UIButton *btnBack;
}
#property (nonatomic, retain) NSArray *accountDetailsArray;
#property (nonatomic, retain) IBOutlet UILabel *nameLabel;
#property (nonatomic, retain) IBOutlet UILabel *accountLabel;
-(IBAction)sendDetails;
-(IBAction)goBack;
-(void)setLabels:(NSArray *)array;
#end
DetailsToTransfer.m
....
-(void)setLabels:(NSArray *)array {
NSLog(#"Setting Labels");
self.nameLabel.text = [array objectAtIndex:0];
self.accountLabel.text = [array objectAtIndex:1];
}
....
I would like to add that all the properties have been synthesized and that I'm calling the method in the appDelegate properly (i checked using NSLogs)
In AppDelegate.m:
Looks as if you are calling a method on your object before the object has been created. Until you have alloc / init your object, there will be no labels to set text.
Try changing your method to this:
-(void)sendToTransferScreen:(NSArray *)detailsArray {
if (!objDetailsToTransfer) {
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[objDetailsToTransfer setLabels:detailsArray];
[self.window addSubview:objDetailsToTransfer.view];
}
}
The problem might be that you are trying to set the values on a object that hasn't been created yet. I also provided an if statement to prevent the object from being created multiple times.

pushViewController memory leak

I have following code, instrument indicate that the pushViewController method has 32 bytes memory leak on device. Could you please kindly help check what rule I break? Should I change some "retain" to "assign" for declaration? Thanks in advance!
#interface GuideNewsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
#private
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
UITableView *tableView;
NewsListViewController *newsListViewController;
}
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSFetchedResultsController *fetchedResultsController;
#property (nonatomic, retain) UITableView *tableView;
#property (nonatomic, retain, readonly) NewsListViewController *newsListViewController;
#implementation GuideNewsViewController
......
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Member *member = [fetchedResultsController objectAtIndexPath:indexPath];
self.newsListViewController.managedObjectContext = self.managedObjectContext;
self.newsListViewController.title = member.memberName;
self.newsListViewController.author = member;
**// leak here**
[self.navigationController pushViewController:self.newsListViewController animated:YES];
}
......
#end
#interface NewsListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
#private
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
UITableView *tableView;
Member *author;
}
#property (nonatomic, assign) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain) UITableView *tableView;
#property (nonatomic, assign, readonly) NSFetchedResultsController *fetchedResultsController;
#property (nonatomic, assign) Member *author;
#end
Just a guess but, what happen if you change your newsListViewController from retain to assign?
I barely write retain, readonly together.
When does self.newsListViewController be released ? I don't think that point causes problem.
Generally, I pushViewController with following manner.
MySelfViewController *childView = [[MySelfViewController alloc] init];
// set up necessary properties of childView
...
// navigationController will release childView when it pops view controller.
[self pushViewController:childView animated:YES];
// release childView after pushViewController
[childView release];
If childView have to notify self something happened, it can use delegate to notify self.
Edit 1:
An example is as following.
// MySelfViewController.h
#protocol MySelfProtocol <NSObject>
- (void)notifySomethingHappened;
#end
#interface MySelfViewController : UIViewController {
id <MySelfProtocol> _delegate;
}
/// client init childview by pass self as parameter.
/// ex: Inside view controller A, he calls by
/// childView = [[MySelfViewController alloc] initWithDelegate:self];
- (id)initWithDelegate:(id)delegate;
/// other member methods
#end
// MySelfViewController.m
#implement MySelfViewController
- (id)initWithDelegate:(id)delegate
{
if (self = [super init])
{
/// assign policy.
/// childView should not retain parent view or delegate.
/// It is possible to let delegate never run dealloc.
_delegate = delegate;
/// custom initialization
....
}
return self;
}
- (void)someThingHappen
{
[_delegate notifySomethingHappend];
}
#end

Hmm, getting : may not respond to '-presentModalViewController:animated:'

I`m getting this annoying warning :
warning: 'AppDelegate' may not respond to '-presentModalViewController:animated:'
In this implementation file :
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
at the line :
self presentModalViewController:controller animated:YES];
and here is the header file :
#import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate, FlipsideViewControllerDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
UIImageView *infoButton;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) IBOutlet UIView *flipside;
#property (nonatomic, retain) IBOutlet UIImageView *infoButton;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)showInfo:(id)sender;
- (IBAction)changePage:(id)sender;
#end
Obviously there is something I`m not getting, but I would appreciate if someone could help :)
You must persent modal view controllers from an instance of UIViewController (not your application delegate). If you check out the documentation on UIViewController, you can access a sample application using the modal presentation.

Class Delegate does not implement protocol

I`m doing something wrong here but I can't figure out what it is .
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) IBOutlet UIView *flipside;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)showInfo:(id)sender;
- (IBAction)changePage:(id)sender;
#end
AppDelegate.m
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
This is where I`m getting :
warning: class 'AppDelegate' does not implement the 'FlipsideViewControllerDelegate' protocol.
After line :
controller.delegate = self;
My FlipsideViewController.h looks like this :
#import <UIKit/UIKit.h>
#protocol FlipsideViewControllerDelegate;
#interface FlipsideViewController : UIViewController {
id <FlipsideViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#en
Any help would be greatly appreciated :)
It's exactly what the error message says. AppDelegate just doesn't implement the protocol. In your header file, add FlipsideViewControllerDelegate between the brackets (i.e. <UIApplicationDelegate, UIScrollViewDelegate, FlipsideViewControllerDelegate>), and implement the -flipsideViewControllerDidFinish: method.
try adding FlipsideViewControllerDelegate to the appDelegate
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate,FlipsideViewControllerDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}

iPad/iPhone TableVIew help pushing a new view

So we have recently started developing applications for the iPad for our company. Unfortunately none of us here have ever done any iPhone/iPad development so we are just kind of learning on the fly and winging it. Basically our problem is happening when we try to push a view onto the screen when a table row is clicked. Neither of us can figure out why it's not doing it because the code looks 100% correct by anything we can tell. Here is what the didRowSelectedAtIndex: method looks like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
if(pdvController == nil)
pdvController = [[PersonDetailViewController alloc] initWithNibName:#"PersonDetailView" bundle:[NSBundle mainBundle]];
Person *aPerson = [appDelegate.people objectAtIndex:indexPath.row];
pdvController.aPerson = aPerson;
[self.appDelegate.navigationController pushViewController:pdvController animated:YES];
}
and here is the header for the toolbarSearchViewController:
#import <UIKit/UIKit.h>
#import "RecentSearchesController.h"
#import "PersonDetailViewController.h"
#class ToolbarSearchAppDelegate, PersonDetailViewController;
#interface ToolbarSearchViewController : UIViewController <UISearchBarDelegate, UIPopoverControllerDelegate, RecentSearchesDelegate> {
ToolbarSearchAppDelegate *appDelegate;
UIToolbar *toolbar;
UISearchBar *searchBar;
UITableView *resultsTable;
PersonDetailViewController *pdvController;
RecentSearchesController *recentSearchesController;
UITableViewController *resultsTableController;
UIPopoverController *recentSearchesPopoverController;
}
#property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
#property (nonatomic, retain) UISearchBar *searchBar;
#property (nonatomic, retain) IBOutlet UITableView *resultsTable;
#property (nonatomic, retain) ToolbarSearchAppDelegate *appDelegate;
#property (nonatomic, retain) PersonDetailViewController *pdvController;
#property (nonatomic, retain) UITableViewController *resultsTableController;
#property (nonatomic, retain) RecentSearchesController *recentSearchesController;
#property (nonatomic, retain) UIPopoverController *recentSearchesPopoverController;
#end
and the ToolbarSearchAppDelegate header:
#import <UIKit/UIKit.h>
#class ToolbarSearchViewController;
#interface ToolbarSearchAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ToolbarSearchViewController *viewController;
UINavigationController *navigationController;
NSMutableArray *people;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ToolbarSearchViewController *viewController;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) NSMutableArray *people;
#end
and finally the personDetailController.h:
#import <UIKit/UIKit.h>
#class Person;
#interface PersonDetailViewController : UIViewController {
IBOutlet UITableView *tableView;
Person *aPerson;
}
#property (nonatomic, retain) Person *aPerson;
#property (nonatomic, retain) UITableView *tableView;
#end
We are getting pretty annoyed because neither of us can figure out why it's not loading and any help or insight you guys could provide would be amazing. I wasn't sure what you may need to see, so if you need anything more specific, let me know and I will post it. Thanks!
ps. I am not sure how the code tags are going to handle all of Obj-c's quirky syntax, so if you see syntax errors, just ignore them. Assume that it is syntactically correct and will compile and run...
This line looks a little odd:
[self.appDelegate.navigationController pushViewController:pdvController animated:YES];
try and make it
[self.navigationController pushViewController:pdvController animated:YES];
edit summing op, the problem is that you don't have a navigation controller, so you can't push a view controller like that. What you can do, is pushing a view controller modally, using
[self presentModalViewController:pdvController animated:YES];