Passing integer to DetailViewController - ios

I have followed the tutorial Passing Data between View Controllers section Passing Data Forward. My code is:
MasterViewController.h:
-(void)pushViewController: (UIViewController *)detailVC animated:(BOOL)animated;
MasterViewController.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
NSInteger num = indexPath.row;
DetailViewController *detailVC = [[DetailViewController alloc] initWithNumber:indexPath.row];
detailVC.number = num;
[self pushViewController:detailVC animated:YES];
}
I want to pass the integer num to my DetailViewController. But I am getting Thread 1: SIGABRT. What is happening?

MasterViewController doesn't inherit from UINavigationController.
You can't push to the navigation stack if there isn't one.
You can either add one, or present the new VC, depending upon what the design of your GUI is intended to be.
P.S.
WHat's the point in setting the number twice as you are:
NSInteger num = indexPath.row;
DetailViewController *detailVC = [[DetailViewController alloc] initWithNumber:indexPath.row];
detailVC.number = num;
You either set it with init or set it directly, you're doing it both ways

My best guess (without seeing your #interface for DetailViewController) is that it's simply a matter of the detailVC instance going away after you put it on screen.
Simple solution: use an instance variable instead:
#interface WhateverClassYouAreIn
#property (nonatomic, strong) DetailViewController *detailVC
#end
// ...
self.detailVC = [[DetailViewController alloc] initWithNumber:indexPath.row];
The reason this is happening is that without the instance variable, the storage duration of the detailVC is the scope of the method in which it is declared. In general, any VC that goes on screen should be held 'strongly' by something else, all the way back to the root vc.

Is the method "initWithNumber" defined in DetailViewController.h ?
#interface DetailViewController : UIViewController
- (instancetype)initWithNumber:(NSInteger)number;
#end

Related

How to insert values into a UITableViewCell from another view controller

I have a UITableView with 10 cells in it. I have a UIButton & UILabel inside all the 10 cells. Now if I select a UIButton in the 2nd cell, I will navigate to another page and select some values (string values). Now, I want that string to be placed in the UILabel of 2nd cell. Likewise, if i select the UIButton on 5th cell, I will navigate to another page, select some string and that string has to be placed in the UILabel of the 5th cell. How do I insert the values inside the UILabel of that particular cell.
Ideas are appreciated.
I'd recommend setting that second view controller to contain an instance of the first so you can then manipulate the first view controller's data array from that second view.
For example, in the section of the FirstViewController where you create the SecondViewController to perform the push (since as you said in the comments, you're pushing the view controller using a UINavigationController), also make the SecondViewController contain an instance of the FirstViewController.
FirstViewController .m:
// Method to handle the push transition
- (void)buttonPressed:(UIButton*)button {
// Set the view controller's `selectedRow` property
// to contain the selected row number as contained in
// the button tag in this particular scenario
self.selectedRow = (int)button.tag;
// Create an instance of SecondViewController
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
// Pass the view controller along by setting
// the secondViewController's firstViewController
// property to contain self, i.e. the current
// instance of the FirstViewController
secondViewController.firstViewController = self;
// Push to the SecondViewController
[self.navigationController pushViewController:secondViewController animated:YES];
}
And in the .h of SecondViewController, add the firstViewController property such that it's publicly accessible from the FirstViewController.
SecondViewController .h:
#import "FirstViewController.h"
#interface SecondViewController : UIViewController
#property (weak, nonatomic) FirstViewController *firstViewController;
Then in the .m of SecondViewController, you can manipulate the array acting as your table's data source in FirstViewController as needed, before popping the view.
SecondViewController .m:
// Method called after the string is selected
- (void)stringSelected:(NSString*)string {
// Update the array to contain the string
// (or do whatever manipulation to whatever data
// structure is fitting in your particular case)
[self.firstViewController.array replaceObjectAtIndex:self.firstViewController.selectedRow withObject:string];
// To go back to the previous view, pop the view controller
[self.navigationController popViewControllerAnimated:YES];
}
Also, you'll want the UITableView's data source array and selectedRow property to be contained in the .h of FirstViewController so it's publicly accessible from SecondViewController.
FirstViewController .h:
#property (weak, nonatomic) NSMutableArray *array;
#property (nonatomic) int selectedRow;
And in the .m of FirstViewController you want to make sure to reload the table data upon return from the other view so the UITableView reflects the updated data.
FirstViewController .m:
- (void)viewWillAppear:(BOOL)animated {
[self.table reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellName" forIndexPath:indexPath];
if(cell == nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CellName"];
}
//Assigning the contents of cell
cell.labelName.text = [NSString stringWithFormat:#"%#", [self.arrCategoryTitle objectAtIndex:indexPath.row]];
[cell.buttonName addTarget:self action:#selector(buttonNamePressed:) forControlEvents:UIControlEventTouchDown];
// Configure the cell...
return cell;
}
- (void)buttonNamePressed:(UIButton *)sender
{
CGPoint swipepoint = sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:swipepoint toView:self.tableName];
NSIndexPath *indexPath = [self.tableName indexPathForRowAtPoint:rootViewPoint];
NSLog(#"%#", indexPath);
[self.tableName cellForRowAtIndexPath:indexPath];
self.defaultRowId = indexPath.row;
[self performSegueWithIdentifier:#"viewControllerIdentifierName" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailsViewController *detailsViewController = (DetailsViewController *)[(UINavigationController *)segue.destinationViewController topViewController];
detailsViewController.defaultrow = self.defaultRowId;
}
Now when you navigate to the secondViewController, set the selecting string value to the property of the firstViewController then reload table.

ViewDidLoad not called from didSelectRowAtIndexPath splitviewController

I am trying to load a single (storyboard id:ClaimReportView) ViewControllers with diff data depending upon the rows clicked on the table view of MasterVC. But the viewDidload is called only once for the VC and hence if i trying to click on diff rows in table view it doesn't call the viewdidload.
.M file
#interface LAMasterViewController ()
{
NSArray *_claimReports;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
LAClaimReport *record = [_claimReports objectAtIndex:indexPath.row];
if ([record.submitted isEqualToNumber:#0] )
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.claimReportViewController = [storyboard instantiateViewControllerWithIdentifier:#"ClaimReportView"];
[self.detailViewController.navigationController setViewControllers:[NSArray arrayWithObject:self.claimReportViewController] animated:YES];
self.claimReportViewController.claimReport = record;
}
}
.h file
#interface LAMasterViewController : UITableViewController
#property (strong, nonatomic) LADetailViewController *detailViewController;
#property (strong,nonatomic) LAClaimReportViewController *claimReportViewController;
#end
What am i missing here? i think i haven't pushed the VC in navigation Stack so ViewDidLoad not called? pls guide.
viewDidLoad is called once when your view is loaded in the first time so there will be no affect in tableView:didSelectRowAtIndexPath: method . Its better you can create your own method and call that method in the tableView:didSelectRowAtIndexPath: method that will work for you.
You can not call viewDidLoad method because it is viewController's life cycle method that can not be called manually, it will be called automatically when view is loaded.

ios push controller view programmatically without storyboards or xib

In ViewControllerA I have a table with three rows of data right now (three messages). Clicking on a row, I'm trying to push to ViewControllerB with the text from one of the rows (the name). Problem I'm having is I'm trying to do everything programmatically, not using storyboard ids or storyboard segues. Is this possible? Here's what I have.
ViewControllerA m.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Messages *cell = (Messages *)[tableView cellForRowAtIndexPath:indexPath];
NSString *from = [NSString stringWithFormat:#"%#", cell.from.text];
ViewControllerB *VCB = [[ViewControllerB alloc]init];
VCB.from = from;
[self.navigationController pushViewController:VCB animated:YES];
//using this^ creates a new view controller with a black screen but passes the variable
//[self performSegueWithIdentifier:#"pushMessage" sender:self];
using this^ shows my ViewControllerB perfectly but the value isn't set
}
ViewControllerB h.
#property (strong, nonatomic) NSString *from;
ViewControllerB m.
-(void)viewDidLoad{
NSLog(#"%#", from);
}
Is there a way to do this without initiating a new viewcontrollerB in VCA and getting the right push animation?
I believe I understand what you're asking and you are almost on the right track here. You said that it works when you use [self performSegueWithIdentifier:#"pushMessage" sender:self]; but the value is not set.
So, declare an NSString "from" in your header and in the previous code delete the [self.navigationController pushViewController:CVC animated:YES];
//ViewControllerA.h
#property (copy, nonatomic) NSString *from;
//ViewControllerA.m
#synthesize from;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Messages *cell = (Messages *)[tableView cellForRowAtIndexPath:indexPath];
from = [NSString stringWithFormat:#"%#", cell.from.text];
[self performSegueWithIdentifier:#"pushMessage" sender:self];
}
Then you need to use the prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//ViewControllerA.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"pushMessage"]) {
ViewControllerB *vcb = [segue destinationViewController];
[vcb setFrom:[self from]];
}
}
EDIT
Well, I feel like this is a really wrong way to do it. But, this will satisfy all of your conditions asked in the question. I have checked and this does in fact work:
First, Go to your storyboard, click on the View Controller B, click on the identity inspector and change the Storyboard ID. Also click the check that says "Use Storyboard ID".
In your ViewControllerA:
//ViewControllerA.h
- (void)callViewControllerWithCellString:(NSString *)str;
//ViewControllerA.m
- (void)callViewControllerWithCellString:(NSString *)str
{
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
SecondViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerB"];
[vc setFrom:str];
[[self navigationController] pushViewController:vc animated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Messages *cell = (Messages *)[tableView cellForRowAtIndexPath:indexPath];
NSString *from = [NSString stringWithFormat:#"%#", cell.from.text];
[self callViewControllerWithCellString:from];
}
So lets see if i understand this.
You want the text from the clicked row passed to the view controller that you want to present
You want to do this without using storyboards
Sounds simple enough. Check this out.
// I actually recommend this option
// Simply initialize your view controller with the text that you want to pass
ViewControllerB * VCB = [[ViewControllerB alloc] initWithText:from];
// For this to work, you'll have to declare a new init method in your .h file like this
-(void)initWithText:(NSSTring *)text;
// You should also declare an iVar to store your text
NSString * fromText;
// and implement it in your .m file like this
-(void)initWithText:(NSString *)text
{
self = [super init];
if (self)
{
fromText = text;
}
return self;
}
// Or, you could do this.
// In ViewControllerB.h, create a property
#property (nonatomic, strong) NSString * fromText;
// In ViewControllerB.m, synthesize it
#synthesize fromText;
// Then you can pass that property a value like this
ViewControllerB * VCB = [[ViewControllerB alloc] init];
VCB.fromText = from;
Variables shoudl be lower case.
Your ViewControllerB should be displaed. Do you create all view elements in there programatically too? If you don't then you better load it from a XIB or from the storyboard. You can do that without segueing to it.
But I guess it is much easer to overwrite the perpareForSegue: method and set the from property there. That is what perpareForSegue is made for when you need to do some customization programatically while using storyboards for all the easy stuff.
First off, you should check your code. You are pushing a viewController named CVC onto the navigation controller. I assume this is a typo.
Did you override ViewControllerB "initWithNibName:bundle:" as I see you are calling init. How would the ViewController know what nib to load if you didn't?
Also, it is recommended that you call [super viewDidLoad] in your viewDidLoad call.
You can push views programmatically as well.
You seem to do following
ViewControllerB *VCB = [[ViewControllerB alloc]init];
VCB.from = from;
[self.navigationController pushViewController:CVC animated:YES];
Which means you want to push VCB and not CVC. This change should fix your problem.
You can also put a breakpoint in viewDidLoad to verify your new ViewController is loaded.
If you are using storyboard for VCB as well, I think you should do following --
ViewControllerB *VCB = [self.storyboard instantiateViewControllerWithIdentifier:#"VCBIdentifier"];
where VCBIdentifier is the identifier to your ViewControllerB marked in Identity Inspector -> StoryBoardID.
With your current init, I think, it is creating a default viewcontroller which is blank/empty.

Set property of another UIViewController

In my DetailViewController.h I've declared the property * type as seen below:
#interface DetailViewController : UIViewController
#property (strong, nonatomic) NSString *type;
#end
Then I got a ListViewController which is a UIViewController containing a TableView.
The ListViewController.h looks like this:
#interface ListViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#end
In my ListViewController.m I want to change the value of * type a soon I switch the View (view switching is working fine).
I'm doing this by adding the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UINavigationController * navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailController"];
DetailViewController * detailController = [[DetailViewController alloc] init];
detailController.type = #"video";
NSLog(#"Type: %#", detailController.type);
[self.navigationController pushViewController:navigationController animated:YES];
}
When I put a breakpoint at the second line in this block/function I get detailController with in it * type.
When I NSLog this a few lines later it return "(null)".
The View is changed but but the type is not set.
I can't figure out what I'm doing wrong?
Someone who has a solution for this or can point me to something we're this is explained?
Because I tried searching for the answer but I couldn't find anything equivalent to my problem, or I used the wrong search terms..
in your didSelectRowAtIndexPath: method change the following lines`
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * detailController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailController"];;
detailController.type = #"video";
NSLog(#"Type: %#", detailController.type);
[self.navigationController pushViewController: detailController animated:YES];
}`
in your storyboard select DetailViewController and give storyboard id as "DetailController". Make sure your ListViewController is embed in UINavigationController, you can do that by selecting ListViewController and go to editor->Embed in->Navigation Controller. Follow these steps and your problem will be solved.
OR you can give segue from your ListViewController to DetailViewController and use
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //create tableview outlet
[segue.destinationViewController setType:#"video"];
}
What is this for?
UINavigationController * navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailController"];
Why are you pushing a navigation controller inside a navigation controller?
[self.navigationController pushViewController:navigationController animated:YES];
detailController was not used or displayed.
Try this instead:
[self.navigationController pushViewController:detailController animated:YES];
Look at the last line - you are telling the nav controller to push itself!
It should be like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * detailController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailController"];
detailController.type = #"video";
NSLog(#"Type: %#", detailController.type);
[self.navigationController pushViewController:detailController animated:YES];
}
Is the navigation controller and the alloc-init method both instantiates the save view controller??
If yes, try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * detailController = [[DetailViewController alloc] init];
detailController.type = #"video";
NSLog(#"Type: %#", detailController.type);
[self.navigationController pushViewController:detailController animated:YES];
}

calling didSelectRowAtIndexPath from other ViewController not responding iPad

I want to change the selected row in the table (in MasterViewController) from a button on a detailViewController.
I know that I cannot call didSelectRowAtIndexPath because it is a delegate method. I tried using selectRowAtIndexPath but that does not invoke the didSelectRowAtIndexPath nor the willSelectRowAtIndexPath method. As suggsted here,
https://stackoverflow.com/a/7496926/1050722 it is possible to some new method and then call that method from the other ViewController (detailViewController), but how should that method look like?
Here is some code:
MasterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self pushDetailViewController:indexPath];
}
-(void)pushDetailViewController:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
[self.navigationController pushViewController:self.detailViewController animated:YES];
NSLog(#"pushDetailviewC and index %#", indexPath);
}
DetailViewController.m
-(IBAction)nextItem
{
MasterViewController *mVc = [[MasterViewController alloc] init];
[mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]];
}
Can someone give me an example to fix this? Thanks.
EDIT: Here is the new code
MasterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self pushDetailViewController:indexPath];
}
-(void)pushDetailViewController:(NSIndexPath *)indexPath{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
self.detailViewController.mVc = self;
[self.navigationController pushViewController:self.detailViewController animated:YES];
NSLog(#"pushDetailviewC and index %#", indexPath);
}
DetailViewController.h
#import <UIKit/UIKit.h>
#import "MasterViewController.h"
#interface DetailViewController : UIViewController <UISplitViewControllerDelegate, UITableViewDelegate>
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (nonatomic, retain) MasterViewController *mVc;
DetailViewCOntroller.m
-(IBAction)test{
//MasterViewController *mVc = [[MasterViewController alloc] init];
[self.mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]];
}
It does not call the NSLog part of the method and it is not selecting the row...
This answer is iPad specific as per your question.
The iPad version of the Master/Detail template uses a UISplitViewController two split the screen as you see it. This UISplitViewController instance keeps references to both of your navigations controllers. Which keep reference to your topmost view controllers. Here is sample code, do not use it verbatim except to test since it contains no error checking.
// Picking some arbitrary row
NSUInteger desiredRow = 3;//???
// The Master navigation controller is at index zero in the template, you can check in your app delegate's didFinishLaunching: method
UINavigationController *masterController = [self.splitViewController.viewControllers objectAtIndex:0];
// Next we want a reference to the topmost viewController again you shouldn't assume it's a UITableViewController
UITableViewController *tableController = (UITableViewController *)masterController.visibleViewController;
// Next we get the reference to the tableview in-question
UITableView *tableView = tableController.tableView;
// We translate our desired row to an index path
NSIndexPath *path = [NSIndexPath indexPathForRow:desiredRow inSection:0];
// We select the row , again this will not call didSelectRowAtIndexPath:
[tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone];
// We call didSelectRowAtIndexPath: on the tableview's delegate
[tableView.delegate tableView:tableView didSelectRowAtIndexPath:path];
When I put this code in an IBAction method in the detail view controller the linked button performs the appropriate actions, both selects the row and pushes a VC, as if I had selected the row by touch.
In DetailViewController.h add a property to the object that points to the MasterViewController. You will also need to import the MasterViewController.h so something like
#import "MasterViewController"
...
#property (nonatomic, retain) MasterViewController *mVc;
...
Don't forget in DetailViewController.m to #synthesize mVc;
Now in your pushDetailViewController you add a reference to join the two objects
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
[self.detailViewController.mVc = self]; // New line
[self.navigationController pushViewController:self.detailViewController animated:YES];
Then in the DetailViewController you reference the object
-(IBAction)nextItem{
[self.mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]];
}
I think another issue you were having was alloc'ing a new version of mVC each time you click the nextItem button. IOS will happily let you make lots of MasterViewController objects and makes a new one each time you alloc it. You wanted just to get a handle to your original MasterViewController.
Another approach would be to explore using the parent methods to reference the MasterViewController. But I wanted to show you how to do this with an explicit property since I thought it would be clearer.
Also, this may or may not fix all of your issues, but it hopefully at least shows you how to talk back to the actual MasterViewController.

Resources