i need to pass data between to Views. I really can't figure out why is not working. Variable selectedRow in SecondViewController is always empty.
FirstViewController.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
SecondViewController *newView = [[SecondViewController alloc] init];
newView.selectedRow = [tablelist objectAtIndex:indexPath.row];
newView.title = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[self.navigationController pushViewController:newView animated:YES];
}
SecondViewController.h
#interface SecondViewController : UIViewController
{
NSString *title,*selectedRow;
}
#property(nonatomic,retain) NSString *title;
#property (nonatomic,retain) NSString *selectedRow;
#end
SecondViewController.m
#synthesize title,selectedRow;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 64, 320, 370)];
textView.text = selectedRow;
textView.editable = FALSE;
[self.view addSubview:textView.text];
}
return self; }
I'm struggling for hours. I really cant figure why.
Any help?
Thank you
The problem is that you are looking at the selectedRow value before you set the property. You can't set the selectedRow property until after the call to init completes.
Since your use of the property is to setup the view controller's view, you should move that code to viewDidLoad where it belongs. As a rule, you should not access self.view in the init method of a view controller.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 64, 320, 370)];
textView.text = selectedRow;
textView.editable = FALSE;
[self.view addSubview:textView.text];
}
I used savestrings to pass data to the next viewcontroller. The only thing you need to do is import the viewcontroller from where the data comes in the viewcontroller.
You can use this in the first viewcontroller
//1e savestring
NSString *saveString1 = emailfield.text;
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
[defaults1 setObject:saveString1 forKey:#"saveString1"];
[defaults1 synchronize];
ANd this in the viewcontroller where you need to load data
//First load string
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSString *loadString1 = [defaults1 objectForKey:#"saveString1"];
[emailfield setText:loadString1];
if you are using storyboard to get a viewcontroller ,you can try this.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [ UIStoryboard storyboardWithName:#"Main" bundle:nil ];
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"test" ];
vc.title = #"something";
[self.navigationController pushViewController:detailView animated:YES];
}
Related
I have a project the same menu as this but with submenu that the same slide animation. I create a xib file name it as SecondMenu.xib. The file owner is UIViewController and name it as SecondMenuController. If you check the project REFrosted (check the link), there is DEMOMenuViewController(UIViewController). In method didSelectRowAtIndexPath:. I changed it in my code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < 1; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
SecondMenuController *controller = [self.viewControllers objectAtIndex:0];
if ((NSNull *)controller == [NSNull null]) {
controller = [[SecondMenuController alloc] initWithNibName:#"SecondMenu" bundle:nil];
[self.viewControllers replaceObjectAtIndex:0 withObject:controller];
}
controller.view.frame = CGRectMake(-self.view.frame.size.width, 0,self.view.frame.size.width,self.view.frame.size.height);
[self.view addSubview:controller.view];
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
controller.view.frame = CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height);
} completion:^(BOOL finished) {
NSLog(#"Done!");
}];
//[self hideMenu];
}
That code work for me when connecting to submenu(which is SecondMenuController). In my SecondMenuController, I add a button (this will connect to DEMOHomeViewController, and the storyboard identifier is homeController). here's the code of my button
- (IBAction)buttonConnect:(id)sender {
DEMONavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"contentController"];
DEMOHomeViewController *homeViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"homeController"];
navigationController.viewControllers = #[homeViewController];
self.frostedViewController.contentViewController = navigationController;
[self.frostedViewController hideMenuViewController];
}
I get this code in REFrosted before I changed it into my code in didSelectRowAtIndexPath: (as what I have mention above in first code). Why is it I get this error
'NSInvalidArgumentException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: object cannot be nil'
I import all files. As what I have understood, when you import a files that connect to the file owner. This means I have control for every method or function that inside of that file when you instantiate it into my SecondMenuController(this only based on my experienced, just rectify me if I'm wrong. I just want to know how the system work). Please see my code. Did I miss something here? Hoping for you advised on how can i fix this or explanation why I get this error.
DEMONavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"contentController"];
DEMOHomeViewController *homeViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"homeController"];
navigationController.viewControllers = #[homeViewController];
2 days of searching an answer for this. I finally get it.
The reason why the DEMOHomeViewController is nil when I click the button because SecondMenuController didn't connect to DEMOHomeViewController even I instantiate it to SecondMenuController class. I learn now. Here's the code
first code that i do. I add this SecondMenuController.h
#property (nonatomic, weak) UIStoryboard * myself;
#property (nonatomic, strong) REFrostedViewController * ref;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil myStoryBoard:(UIStoryboard *) xStoryBoard ref: (id) xRef;
In SecondMenuController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil myStoryBoard:(UIStoryboard *) xStoryBoard ref: (id) xRef
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.myself = xStoryBoard;
self.ref = xRef;
}
return self;
}
In my button
DEMONavigationController *navigationController = [self.myself instantiateViewControllerWithIdentifier:#"contentController"];
DEMOSecondViewController *secondViewController = [self.myself instantiateViewControllerWithIdentifier:#"homeController"];
navigationController.viewControllers = #[secondViewController];
self.ref.frostedViewController.contentViewController = navigationController;
[self.ref.frostedViewController hideMenuViewController];
Now it's working. All you need to do is to call the initWithNibName: method to the DEMOMenuViewController.
The console log means you are attempting to insert nil object in NSArray, which is not allowed. If for some reason you need to add empty object use (NSArray *)[NSNull null] instead.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *second=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
second.getString=[getArray objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"Action" sender:nil]; }
How to pass data from one view to another,In my app there are some objects i want that when i tap on any cell then that value should be shown on next ViewController which having UILabel?
Create a init method in your SecondViewController as shown below.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withData:(NSString *) data {
if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.data = data;
// write your own code here
}
return self;
}
In your tableview's didSelectRowAtIndexPath row method get the string and then push the controller as shown below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSSting *stringData = #"hi";// get your data and passing to stringData
SecondViewController *secondVC=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil withData:stringData];
[self.navigationController pushViewController: secondVC animated:YES];
}
try this
create an instance variable NSString * sendingText ;// if you want to send text(string)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
sendingText = [getArray objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"Action" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Action"]) // this check is optional if there is only on segue
{
SecondViewController *second= (SecondViewController *)segue.destinationViewController;
second.getString = sendingText;
}
}
in SecondViewController.h
#interface SecondViewController : UIViewController
{
UILabel * exampleLabel;
}
#property NSString * getString;
#end
and in SecondViewController.m
-(void)viewDidLoad
{
exampleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];
[self.view addSubview:exampleLabel];
}
-(void)viewWillAppear:(BOOL)animated
{
exampleLabel.text = getString;
}
I want to perform didselecterowatindexpath method please let me know how to call second viewcontroller from first view controller using below method and passing the data to other view controller :
My code is
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
pun=[tableData objectAtIndex:indexPath.row];
NSLog(#"PUN IS :%#",pun);
appDelegate.matri=pun;
NSLog(#"matriC:%#",appDelegate.matri);
SecViewController *SecView = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
[self.navigationController pushViewController:SecView animated:YES ];
Try this. its a best option.
In your .h file create a method
- (id)initWithNibName:(NSString *)nibNameOrNil Yourvariable:(yourdatatype *)variable Yourvariablex:(yourdatatype *)variablex bundle:(NSBundle *)nibBundleOrNil;
and in your .m file
- (id)initWithNibName:(NSString *)nibNameOrNil Yourvariable:(yourdatatype *)variable Yourvariablex:(yourdatatype *)variablex bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//assign or use data here.
}
return self;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *tempString =[tableData objectAtIndex:indexPath.row];
NSLog(#"PUN IS :%#", tempString);
SecViewController *SecView = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
SecView.usernameString= tempString;
[self.navigationController pushViewController:SecView animated:YES ];
}
// .h file of SecViewController
#import <UIKit/UIKit.h>
#interface SecViewController : UIViewController
#property (nonatomic,strong) NSString *usernameString;
#end
// .m file of SecViewController
#import "ViewController.h"
#interface SecViewController ()
{
}
#end
#implementation SecViewController
#synthesize usernameString;
Hi Mate follow below steps :
Goto SecViewController.h file and create a property of NSMutableArray.
#property(retain,nonatomic) NSMutableArray *newArray;
In tableView:didSelectRowAtIndexPath method write
SecViewController *secView = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
secView.newArray = [tableData objectAtIndex:indexPath.row];
[self.navigationController pushViewController:secView animated:YES ];
In SecViewController print the below line in viewDidLoad Method
NSLog(#"%#",newArray);
In one.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSString *carNumber = [self.carNumberArrayFromPlistFile objectAtIndex:row];
NSString *engineNumber = [self.engineNumberArrayFromPlistFile objectAtIndex:row];
CarInfo *oneCarInfo = [[CarInfo alloc] initWithCarNumber:carNumber engineNumber:engineNumber];
Two *two = [[Two alloc] initWithNibName:#"Two" bundle:nil];
two.car = oneCarInfo;
[self.navigationController pushViewController:two animated:YES];
[oneCarInfo release];
[two release];
}
In two.h
#interface Two : UITabBarController
{
CarInfo *car;
}
#property (nonatomic, retain)CarInfo *car;
And why the car in Two.m is always null? Please help me with this. Thank you guys!
Two.m:
#interface Two ()
#end
#implementation Two
#synthesize car;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.car = nil;
}
- (void)dealloc
{
[super dealloc];
}
#end
#PeterPajchl : you are not supposed to push instance of UITabBarController onto the stack of UINavigationController - check the documentation.
For some reason [tView reloadData] (where tView is a UITableView) does not refresh my UITableView. cellForRowAtIndexPath, numberOfSectionsInTableView and numberOfRowsInSection only get called once - at load. Those methods don't seem to be called after [tView reloadData]. Here's my code:
(AppDelegate.h):
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#class FirstNavViewController;
#class SecondTableViewController;
#interface <appname>AppDelegate : NSObject <UIApplicationDelegate, MBProgressHUDDelegate> {
UIWindow *window;
UITabBarController *rootController;
FirstNavViewController *viewController;
SecondTableViewController *viewController1;
NSMutableData *responseData;
NSMutableArray *blogEntries;
MBProgressHUD *HUD;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#property (nonatomic, retain) IBOutlet FirstNavViewController *viewController;
#property (nonatomic, retain) IBOutlet SecondTableViewController *viewController1;
#property (nonatomic, retain) NSMutableArray *blogEntries;
#end
(AppDelegate.m):
#import "AppDelegate.h"
#import "FirstNavViewController.h"
#import "SecondTableViewController.h"
#import "SBJson.h"
#define TMP NSTemporaryDirectory()
#implementation AppDelegate
#synthesize window = _window;
#synthesize rootController;
#synthesize viewController;
#synthesize viewController1;
#synthesize blogEntries;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
CGFloat width = self.rootController.view.bounds.size.width;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:#"theme_frame.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];
[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
imageView = nil;
[v release];
responseData = [[NSMutableData data] retain];
blogEntries = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:#"ENTER_JSON_URL_HERE"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSAssert(nil != self.rootController, #"tab bar controller not hooked up!");
BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
if (iPad) {
self.viewController = [[[FirstNavViewController alloc] initWithNibName:#"FirstNavViewController_iPad" bundle:nil] autorelease];
self.viewController1 = [[[SecondTableViewController alloc] initWithNibName:#"SecondTableViewController_iPad" bundle:nil] autorelease];
}
else {
self.viewController = [[[FirstNavViewController alloc] initWithNibName:#"FirstNavViewController_iPhone" bundle:nil] autorelease];
self.viewController1 = [[[SecondTableViewController alloc] initWithNibName:#"SecondTableViewController_iPhone" bundle:nil] autorelease];
}
self.rootController.viewControllers = [NSArray arrayWithObject:self.viewController];
self.rootController.selectedIndex = 0;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
self.window.rootViewController = self.rootController;
#else
[self.window addSubview:rootController.view];
#endif
[self.window makeKeyAndVisible];
HUD = [[MBProgressHUD alloc] initWithView:viewController.view];
[viewController.view addSubview:HUD];
[HUD show:NO];
// Regisete for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;
HUD.labelText = #"Loading";
return YES;
}
#pragma mark NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[HUD hide:YES];
[connection release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSMutableArray *allBlogEntries = [responseString JSONValue];
[viewController1 setBlogEntries:allBlogEntries];
[responseString release];
[HUD hide:YES];
}
- (void)dealloc
{
[_window release];
[rootController release];
[viewController release];
[viewController1 release];
[super dealloc];
}
#end
(FirstNavViewController.h):
#import <UIKit/UIKit.h>
#interface FirstNavViewController : UIViewController {
UINavigationController *navController;
}
#property (nonatomic, retain) UINavigationController *navController;
#end
(FirstNavViewController.m):
#import "FirstNavViewController.h"
#import "SecondTableViewController.h"
#implementation FirstNavViewController
#synthesize navController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
UITabBarItem *tabBarItem = [self tabBarItem];
UIImage *tabBarImage = [UIImage imageNamed:#"blog.png"];
[tabBarItem setImage:tabBarImage];
[tabBarItem setTitle:#"Blog"];
}
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
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
navController = [[UINavigationController alloc] initWithRootViewController:self];
SecondTableViewController *secondViewController = [[SecondTableViewController alloc] initWithNibName:#"BlogOverviewViewController_iPhone" bundle:nil];
[navController pushViewController:secondViewController animated:NO];
[blogOverviewViewController release];
[self.view addSubview:navController.view];
}
- (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
(SecondTableViewController.h):
#import <UIKit/UIKit.h>
#import "SBJson.h"
#interface SecondTableViewController : UIViewController {
NSMutableArray *blogEntries;
IBOutlet UITableView *tView;
}
#property (nonatomic, retain) NSMutableArray *blogEntries;
#property (nonatomic, retain) NSArray *arryData;
#property (nonatomic, retain) IBOutlet UITableView *tView;
#end
(SecondTableViewController.m):
#import "SecondTableViewController.h"
#import "ThirdDetailViewController.h"
#import "NSString+HTML.h"
NSString *convertedString;
#implementation SecondTableViewController
#synthesize arryData;
#synthesize tView;
-(NSMutableArray*)blogEntries {
return [[blogEntries retain] autorelease];
}
-(void)setBlogEntries:(NSMutableArray*)newBlogEntries {
if(newBlogEntries != blogEntries) {
[newBlogEntries retain];
[blogEntries release];
blogEntries = newBlogEntries;
[tView reloadData];
}
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [blogEntries count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSDictionary *aBlogEntry = [blogEntries objectAtIndex:[indexPath row]];
NSArray *bPosts = (NSArray *)[aBlogEntry objectForKey:#"posts"];
NSString *stringToConvert = [bPosts valueForKey:#"title_plain"];
NSString *convertedString = [stringToConvert stringByConvertingHTMLToPlainText];
cell.textLabel.text = convertedString;
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
NSString *stringToConvert1 = [bPosts valueForKey:#"excerpt"];
NSString *convertedString1 = [stringToConvert1 stringByConvertingHTMLToPlainText];
cell.detailTextLabel.text = convertedString1;
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ThirdDetailViewController *detailViewController = [[ThirdDetailViewController alloc] initWithNibName:#"BlogContentViewController_iPhone" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
NSDictionary *aBlogEntry = [blogEntries objectAtIndex:[indexPath row]];
NSArray *bPosts = (NSArray *)[aBlogEntry objectForKey:#"posts"];
NSString *stringToConvert = [bPosts valueForKey:#"title"];
NSString *convertedString = [stringToConvert stringByConvertingHTMLToPlainText];
[contentViewController changeTitleTextLabel:convertedString];
NSString *stringToConvert1 = [bPosts valueForKey:#"content"];
NSString *convertedString1 = [stringToConvert1 stringByConvertingHTMLToPlainText];
NSString *newConvertedString1 = [convertedString1 stringByReplacingOccurrencesOfString: #"\n" withString:#"\n\n"];
[detailViewController changeContentTextLabel:newConvertedString1];
[tableView deselectRowAtIndexPath: indexPath animated: YES];
}
- (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
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
blogEntries = [[NSMutableArray alloc]init];
self.title = #"Blog";
[self.navigationItem setHidesBackButton:YES animated:NO];
}
- (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);
}
- (void)dealloc {
[blogEntries release];
[super dealloc];
}
#end
Does anyone know what the problem is? I am stuck on why the UITableView will not reloadData.
EDIT: Small background: I am using a UITabBarController (AppDelegate) with a UIViewController in it with a UINavigationController as a subview of that UIViewController (FirstNavViewController). The UINavigationController then has a subview with another UIViewController in it. The latter UIViewController (SecondTableViewController) includes a UITableView.
I've just been having this issue. Turned out to be a threading problem for me. For those those who end up here after a search, here is my quick fix:
[tView performSelectorOnMainThread:#selector(reloadData)
withObject:nil
waitUntilDone:false];
It seems reloadData needs to be called on the main thread. I hope this helps (^_^)
In your viewDidLoad method, add: tView.delegate = self;
Your view setup might be the cause of your problems.
I don't really understand the details of your implementation but right now you are adding that SecondTableViewController to both the tab bar controller and then on the stack of a navigation controller in your FistNavViewController viewDidLoad method.
Create the navigation controller with FistNavViewController as its rootViewController, then add the navigation controller as the first view controller to your UITabBarViewController
(code here is typed from memory so please excuse any typos)
FirstNavViewController *vc = [[FirstNavViewController alloc] initWithNibName:#"nibname" andBundleName:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
UITabBarController *tbc = [UITabBarController setViewControllers:[NSArray arrayWithObjects:nc, otherVc1Example, otherVc2Example, nil] animated:NO];
In your FirstNavViewController viewDidLoad method, you can then instantiate SecondTableViewController and push it onto the stack with
[self.navigationController pushViewController:secondTableViewController animated:YES];
Finally, within that nav controller you need to make sure you have your UITableView setup correcly either in Interface Builder (by connecting the datasource and delegate outlets to the file owner) or in code by manually setting the tableview delegate and datasource to self.
Bottom line is everything you are trying to do above is WAY easier and less error prone if you use Interface Builder. In MainWindow.xib simple add a tab bar controller and under it add navigation controllers and view controllers as you see fit. It should work "pretty much" out of the box.
Good luck.
Rog