How can I pass data from UINavigationController to The root UITableViewController?
I have implemented the ECSlidingViewController (https://github.com/edgecase/ECSlidingViewController). User selects one of the cells in the menu that correspond to different urls I want to display information from on my tableView that sitts on top of the UINavigationController. (u know the default combination that u get my dragging UINavigationController to ur storyboard). I am able to get the data from the sliding menu to my navigationController now I am trying to pass that same info on my tableview?
In my menu I have:
UINavigationController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"NavigationTop"];
newTopViewController = [(NavigationTopViewController*)newTopViewController initWithCinema:self.myCinema];
In UINaviationController:
- (id)initWithCinema:(Cinema *)cinema {
self = [super init];
if(self) {
_myCinema = [[Cinema alloc] init];
_myCinema = cinema;
}
return self;
}
- (void) viewDidLoad {
[super viewDidLoad];
// this log works I get the info to here.
NSLog(#"url(navigation):%#", self.myCinema.cinemaURL);
//MoviesTableViewController *moviesTableViewController = [[MoviesTableViewController alloc] initWithCinema:self.myCinema];
//UITableViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"MoviesTable"];
//NavigationTopViewController *newTopViewController = [[NavigationTopViewController alloc] initWithCinema:self.myCinema];
//newTopViewController = [(MoviesTableViewController *)newTopViewController initWithCinema:self.myCinema];
//[self performSegueWithIdentifier:nil sender:self.myCinema];
[self prepareForSegue:nil sender:self.myCinema.cinemaURL];
}
In my UITableView:
- (void)setCinema:(Cinema *)cinema {
// works here too
NSLog(#"Table(setCinema):%#", cinema.cinemaURL);
self.myCinema = [[Cinema alloc] init];
if(!cinema) {
cinema.cityIndex = kAstanaIndex;
cinema.name = kKeruen;
cinema.nameForText = kKeruenText;
cinema.cinemaURL = kKeruenURL;
cinema.cinemaURLTomorrow = kKeruenURLtomorrow;
}
self.myCinema = cinema;
// works here too!!!
NSLog(#"Table(myCinema):%#", self.myCinema.cinemaURL);
}
However its gone in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// set delegate to self
self.tableView.delegate = self;
// set loading theater's url
// does not work here: I GET NULL !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NSLog(#"url(moviesTable):%#", self.myCinema.cinemaURL);
_model = [[MovieModel alloc] initWithURL:self.myCinema.cinemaURL];
}
None of the methods I have tried (commented in Navigation worked...) at least for me. Please give me any suggestions. Thank you in advance.
UINavigationController does not hold any data, but rather a stack of view controllers. I'd recommend you check out frameworks such as the free Sensible TableView. The framework will automatically handle detail view generation and passing data between them. Saves me tons of development time in my projects.
Related
I'm new i iOS and Objective-C world, I want to create an example app to learn something but i find some problems in my program.
I Create a TableViewController with Books names, and after touch a book I want see a some more inforamtion about this book.
So I created book class what is a view controller, i created a some Labels inside this class with some text.
NSLogs works fine. After touch the record, apps pushing to new view controller from table view controller worsk good, but I can't see any content over there instead of white bg and back button at the top.
this is book class:
#import "BooksViewController.h"
#interface BooksViewController ()
#end
#implementation BooksViewController
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if(self){
self.title = self.bookName;
self.view.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *bookNameLabel = [[UILabel alloc] init];
bookNameLabel.text = self.bookName;
bookNameLabel.frame = CGRectMake(10, 10, 300, 50);
[self.view addSubview:bookNameLabel];
UILabel *authorNameLabel = [[UILabel alloc] init];
authorNameLabel.text = self.authorName;
authorNameLabel.frame = CGRectMake(50, 50, 300, 40);
[self.view addSubview:authorNameLabel];
UILabel *bookDescLabel = [[UILabel alloc] init];
bookDescLabel.text = self.bookDesc;
bookDescLabel.frame = CGRectMake(50, 50, 300, 40);
[self.view addSubview:bookDescLabel];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
this is a methods in table view controller witch I use to create a books inforamtion window:
- (void)viewDidLoad {
[super viewDidLoad];
self.bookNames = #[#"Pan Tadeusz", #"Potop", #"Lalka", #"Uczta dla wron", #"Symfonnia C++"];
self.authorsName = #[#"Adam Mickiewicz", #"Henryk Sienkiewicz", #"Bolesław Prus", #"George R.R Martin", #"Jerzy Greborz"];
self.bookDescs = #[#"Opis Pan Tadeusz", #"Opis Potop", #"Opis Lalka", #"Opis Uczta dla wron", #"Opis Symfonnia C++"];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Książka na pozycji %ld tapped",indexPath.row);
BooksViewController *bookVC = [[BooksViewController alloc] init];
bookVC.bookName = self.bookNames[indexPath.row];
bookVC.authorName = self.authorsName[indexPath.row];
bookVC.bookDesc = self.bookDescs[indexPath.row];
NSLog(#"Nazwa wybranej książki: %#",bookVC.bookName);
[self.navigationController pushViewController:bookVC animated:YES];
}
You're doing this wrong.
First, create an iVar that will save the Indexpath that the user selected, for that, simply add an NSIndexPath variable at the very top of your .m file.
#implementation yourControllerNameHere (){ //In your code you will have your controller name, just add the NSIndexpath ;)
NSIndexPath *selectedPath;
}
You then need to perform a segue when you select a tableview cell, for that, replace your -didSelectRowAtIndexPath method with this :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Książka na pozycji %ld tapped",indexPath.row);
selectedPath = indexPath; //We're saving the selected path to our instance variable ! This is very important otherwise we can't find it again.
[self performSegueWithIdentifier:#"fromBooksToDetail"];
}
and add the -prepareForSegue method in your .m file ; it should be there when you first created it ! just find it and add the following
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"fromBooksToDetail"]){
BooksViewController *bookVC =(BooksViewController*)segue.destinationViewController;
bookVC.bookName = self.bookNames[selectedPath.row];
bookVC.authorName = self.authorsName[selectedPath.row];
bookVC.bookDesc = self.bookDescs[selectedPath.row];
NSLog(#"Nazwa wybranej książki: %#",bookVC.bookName);
}
}
Now this will NOT work unless you add a segue link between your two viewcontrollers in storyboard, so simply open your storyboard and, using a right clic or a ctrl+left clic, drag your mouse from your TableViewController to your BooksController. Don't forget to give it the right identifier in the Attributes Inspector on the right panel !
Note that I wouldn't have named them like that ; BooksViewController has more sense if it's the TableViewController name, and "BookDetailViewController" for the detail page. But that's just a detail.
Once you have the segue link, the performSegue call and the prepareForSegue method, you'll be all set ;)
You need to learn how to debug. Try putting NSLog(#"bookname: %#", self.bookName) in your viewDidLoad and see if you print anything. (Most probably not)
I'm not 100% sure how the view life cycle works when you use alloc init for a view controller. But my bet is that the properties are not set there. Try moving bookNameLabel.text = self.bookName; to viewWillAppear instead of viewDidLoad
You should use storyboards instead. It's super easy.
Also, your initWithNibName will never get called. You are using the init-method.
I work in project need calendar view with events , i try many libraries but finally i decide to use kal library as its have ability to add events
Calendar.h
#import "Kal.h"
#import "NSDate+Convenience.h"
#import "EventKitDataSource.h"
#interface Calendar : UIViewController<WebService_Delegate , UITableViewDelegate >
{
KalViewController *kal;
id dataSource;
}
Calendar.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Caledar";
kal = [[KalViewController alloc]initWithSelectionMode:KalSelectionModeSingle];
kal.selectedDate = [NSDate dateStartOfDay:[NSDate date]];
kal.delegate = self;
kal.view.frame = CGRectMake(0, 65, kal.view.frame.size.width, kal.view.frame.size.height);
[kal showAndSelectDate:[NSDate date]];
//navController = [[UINavigationController alloc]initWithRootViewController:kal];
// [self.view addSubview:navController.view];
[self initVariable];
[self getEvents];
dataSource = [[EventKitDataSource alloc] init];
kal.dataSource = dataSource;
[self.view addSubview:kal.view];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Display a details screen for the selected event/row.
EKEventViewController *vc = [[EKEventViewController alloc] init];
vc.event = [dataSource eventAtIndexPath:indexPath];
//[vc setEvent:[events_array objectAtIndex:indexPath.row]];
vc.allowsEditing = NO;
[navController pushViewController:vc animated:YES];
}
how can i pass data to dataSource to display it
here how its look like
i need set events list to my events list , i got event duplicated , its read from my calendar
thank you
You need to implement the KalDataSource protocol in an object and set that object as the datasource of your kal object. The protocol can be found here https://github.com/klazuka/Kal/blob/master/src/KalDataSource.h
Add the KalDataSource protocol to your header file
<WebService_Delegate , UITableViewDelegate, KalDataSource>
In the init method of your Calendar object set
kal.datasource = self
Implement the KalDataSource methods in your object
I tried to implement stripe into an iOS app through its online documentation. Everything good so far, now pushing the paymentView onto my navigation controller stack I get a completely broken screen. Thought it'd be a problem with the stripe view but when I do not log in (see code below - no identification token given) and the login screen is being pushed instead, it is completely black too. It cant be a problem with that view cause it loads just fine if I push the login view from another view before this one.
So why does pushing view via the buyButtonAction below give me black / fucked up screens?!
Ive been on this for hours.. nothing seems to work.
A pic:
the important code part:
#interface PaymentViewController ()
#end
#implementation PaymentViewController
#synthesize stripeCard = _stripeCard;
#synthesize stripeView;
#synthesize passedProductId;
- (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.
self.stripeView = [[STPView alloc] initWithFrame:CGRectMake(15,20,290,55)
andKey:#"pk_test_45mqixOu8N9S4lQ6cdn1OXBD"];
self.stripeView.delegate = self;
[self.view addSubview:self.stripeView];
}
And the call:
-(void)buyButtonAction:(id)sender
{
tokenClass *tokenObject = [tokenClass getInstance];
NSLog(#"%#", tokenObject.token);
if (tokenObject.token == nil) {
LoginController *loginController = [[LoginController alloc] init];
[self.navigationController pushViewController:loginController animated:YES];
} else {
NSLog(#"%#", tokenObject.token);
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
PaymentViewController *payView = [[PaymentViewController alloc] init];
payView.passedProductId = [[self.productData valueForKey:#"id"] objectAtIndex:hitIndex.row];
NSLog(#"passing %#", payView.passedProductId);
// push payment view
payView.navigationItem.title = #"One-Click-Payment";
[self.navigationController pushViewController:payView animated:YES];
}
}
We can see that there's a view behind the navigation bar. It's an iOS 7 related issue. Add this line to your viewDidLoad:
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
Or change your self.stripeView frame by adding 64 to y:
CGRectMake(15,84,290,55)
Useful link: https://stackoverflow.com/a/18103727/1835155
I've been wondering for a few time now how does Xcode interpret the use of both IB and programmaticaly coded objects.
ex : I initWithStyleGrouped a table in .m but I set plain as the style in the attributes of the tableviewcontroller I am working on. so? I noticed that the code gets over the IB.
It first appears when I had to custom a detail table by insering a header and a UITextField in the first cell which is very easy with IB. But when I run the app, nothing but the template of a plain table.
gnuh??
Thank you for your help.
Cheers,
Louis
EDIT
here is the instantiate of the TableViewController :
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
They should never cross. If you instantiate a table with [[UITableView alloc] initWithStyle:UITableViewStyleGrouped], it creates a new table without going though a NIB. If you instantiate a table from a NIB, it creates an instance using -initWithCoder:.
Added after Update
OK, you are subclassing UITableView. In addition to overriding -initWithStyle:, you will want to override -initWithCoder: or -awakeFromNib.
The basic flow of loading an custom UIView from a NIB.
-initWithCoder: is used to instantiate the object
All NIB connections are make (IBOutlets and IBAction are connected).
-awakeFromNib is send to the object
This means if you set a value in -initWithCoder:, the NIB setting will win; if you set a value in -awakeFromNib, the your code will win.
Be sure to read the Subclassing Notes and Methods to Override sections of UIView.
It all depends on how you initialize the object. If you load the UITableView from your controller's init method and call initWithStyle, that's what your UITableView will be. If you want to use IB's, you'll need to initialize your controller with initWithNibName and have an IBOutlet connection to your view, which has the plain setting.
Well, I am not sure but I think I found a beginning of solution. here are my thoughts.
I consider a IB oriented design.
the compiler will ask for IB to instantiate the view.
But if we created a UITableViewController subclass, then we have all the method refering to the instantiation (correct word?) of this view.
So, in order to avoid this conflict, We can erase the code in the .M which refers to the initialization of the table : initWithStyle and the pragma mark about Table source. we just let the View life cycle needed by any view and the delegate.
I found some exemple using this. here is the .m of a detail view table which is designed with static cells on IB :
#import "PictureListDetail.h"
#implementation PictureListDetail
#synthesize managedObjectContext;
#synthesize currentPicture;
#synthesize titleField, descriptionField, imageField;
#synthesize imagePicker;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// If we are editing an existing picture, then put the details from Core Data into the text fields for displaying
if (currentPicture)
{
[titleField setText:[currentPicture title]];
[descriptionField setText:[currentPicture desc]];
if ([currentPicture smallPicture])
[imageField setImage:[UIImage imageWithData:[currentPicture smallPicture]]];
}
}
#pragma mark - Button actions
- (IBAction)editSaveButtonPressed:(id)sender
{
// If we are adding a new picture (because we didnt pass one from the table) then create an entry
if (!currentPicture)
self.currentPicture = (Pictures *)[NSEntityDescription insertNewObjectForEntityForName:#"Pictures" inManagedObjectContext:self.managedObjectContext];
// For both new and existing pictures, fill in the details from the form
[self.currentPicture setTitle:[titleField text]];
[self.currentPicture setDesc:[descriptionField text]];
if (imageField.image)
{
// Resize and save a smaller version for the table
float resize = 74.0;
float actualWidth = imageField.image.size.width;
float actualHeight = imageField.image.size.height;
float divBy, newWidth, newHeight;
if (actualWidth > actualHeight) {
divBy = (actualWidth / resize);
newWidth = resize;
newHeight = (actualHeight / divBy);
} else {
divBy = (actualHeight / resize);
newWidth = (actualWidth / divBy);
newHeight = resize;
}
CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
UIGraphicsBeginImageContext(rect.size);
[imageField.image drawInRect:rect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save the small image version
NSData *smallImageData = UIImageJPEGRepresentation(smallImage, 1.0);
[self.currentPicture setSmallPicture:smallImageData];
}
// Commit item to core data
NSError *error;
if (![self.managedObjectContext save:&error])
NSLog(#"Failed to add new picture with error: %#", [error domain]);
// Automatically pop to previous view now we're done adding
[self.navigationController popViewControllerAnimated:YES];
}
// Pick an image from album
- (IBAction)imageFromAlbum:(id)sender
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePicker animated:YES completion:nil];
}
// Take an image with camera
- (IBAction)imageFromCamera:(id)sender
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
[self presentViewController:imagePicker animated:YES completion:nil];
}
// Resign the keyboard after Done is pressed when editing text fields
- (IBAction)resignKeyboard:(id)sender
{
[sender resignFirstResponder];
}
#end
available here : enter link description here
What do you think ??
Even though I now alone on this post, I wanted to post the answer !
I finally figured out why my textfield didn't appear which show who's has the hand on the compiler.
I actually subclassed the detail with a class wich implement methods to fill the table source with the coreData.Then, the supposed static cells were actually filled with these methods.
Which shows, to my humble opinion, that the .m overpass the IB.
In post Using initWithNibName changes absolutely nothing, he shows two uses of the same View Nib definition, in the first case, he simply calls alloc/init and the second, he specifies initWithNibName.
So, while this always works:
MyViewController *vctrlr = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[self.navigationController pushViewController:vctrlr animated:YES];
[vctrlr release];
The following works for all the View Controllers I've inherited, but not mine!
TheirViewController *vctrlr = [[TheirViewController alloc] init];
[self.navigationController pushViewController:vctrlr animated:YES];
[vctrlr release];
New to iOS programming, I inherited some code. All the View Controllers' views are defined in IB, but there was inconsistent allocation/init creation of those view controllers. I created a new View Controller and XIB, but it does not work unless I use initWithNibName (it crashes when I push the view controller onto the Nav Controller). I cannot tell how my view controller is different than the others... any hints? I was able to delete the initNibName usage for all the other view controllers in the app except mine.
You can pass any string name to initWithNibName:. You are not just restricted to calling initWithNibName:#"MyClassName" when your class is called MyClassName. It could be initWithNibName:#"MyClassNameAlternateLayout".
This becomes useful if you need to load a different nib depending on what the app needs to do. While I try to have one nib per view controller per device category (iPhone or iPad) whenever possible to make development and maintenance simpler, I could understand if a developer would want to provide a different layout or different functionality at times.
Another important point is that initWithNibName:bundle: is the designated initializer for UIViewController. When you call -[[UIViewController alloc] init], then initWithNibName:bundle: is called behind the scenes. You can verify this with a symbolic breakpoint. In other words, if you simply want the default behavior, it is expected that you can call -[[UIViewController alloc] init] and the designated initializer will be called implicitly.
If, however, you are calling -[[UIViewController alloc] init] and not getting the expected behavior, it's likely that your UIViewController subclass has implemented - (id)init incorrectly. The implementation should look like one of these two examples:
- (id)init
{
self = [super init];
if (self) {
// custom initialization
}
return self;
}
or
- (id)init
{
NSString *aNibName = #"WhateverYouWant";
NSBundle *aBundle = [NSBundle mainBundle]; // or whatever bundle you want
self = [self initWithNibName:aNibName bundle:aBundle];
if (self) {
// custom initialization
}
return self;
}
If you want to work following code:
MyViewController *vctrlr = [[MyViewController alloc] inil];
[self.navigationController pushViewController:vctrlr animated:YES];
Then you should implement following both methods in MyViewController:
- (id)init
{
self = [super initWithNibName:#"MyViewController" bundle:nil];
if (self != nil)
{
// Do initialization if needed
}
return self;
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
NSAssert(NO, #"Init with nib");
return nil;
}