I have two views and the variable is loaded from my TestViewController which is a UITableViewController. Below i have the part of the code where i believe it will be useful.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selected = [[_sectionsArray objectAtIndex:indexPath.row] objectForKey:#"Section"];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.viewController.detailSec = selected;
NSLog(#"%#",selected);
}
At my ViewController the following code is supposed to change my UILabel text to my variable
-(void)setDetailSec:(id)newDetailSec {
if (_detailSec != newDetailSec) {
[_detailSec release];
_detailSec = [newDetailSec retain];
[self configureView];
}
NSLog(#"Step 2 %#",_detailSec);
}
-(void)configureView {
if (self.detailSec) {
self.detailLabelChord.text = [_detailSec description];
NSLog(#"Step 3 %#", _detailSec);
}
As you can see I added NSLogs in order to check whether the if functions are called and whether the variable changes accordingly and it does! But my UILabel will not change!
2012-07-18 22:03:26.077 testapp[17332:11303] Step 3 CHS 21.3x3.2
2012-07-18 22:03:26.078 testapp[17332:11303] Step 2 CHS 21.3x3.2
2012-07-18 22:03:26.079 testapp[17332:11303] Step 1 CHS 21.3x3.2
Any ideas or suggestions? I don't know whether i can upload my project if you need more information.
Thank you in advance
Instead of doing this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selected = [[_sectionsArray objectAtIndex:indexPath.row] objectForKey:#"Section"];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.viewController.detailSec = selected;
NSLog(#"%#",selected);
}
do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selected = [[_sectionsArray objectAtIndex:indexPath.row] objectForKey:#"Section"];
self.viewController.detailSec = selected;
NSLog(#"%#",selected);
}
There is no need to initialize a new viewController from a nib every time you select a row. You already have viewController. When you make a new viewController you are calling setDetailSec on this new controller that hasn't been displayed. You are not calling it on the viewController that you can see with the UILabel.
Related
I am trying to send a string from my TableViewContoller to my LabelViewController in didSelectRowAtIndexPath. The string is the number of the row in my table. In LabelViewController.h I have my UILabel property called *rowNumberLabel.
In my TableViewController, I attempt to set the text of the label in didSelectRowAtIndexPath with the following code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.rowNumber = indexPath.row;
self.numberString = [NSString stringWithFormat:#"%lu", self.rowNumber];
LabelViewController *labelViewController = [[LabelViewController alloc] init];
NSLog(#"The row number is %#", self.numberString);
labelViewController.rowNumberLabel.text = self.numberString;
[self.navigationController pushViewController:labelViewController animated:YES];
}
I am very sure I am looking over something simple but I cannot figure out why the text of the label is not setting when I push labelViewController. Thanks for the help.
The reason is that rowNumberLabel isn't connected right after initializing the LabelViewController.
Create a property in LabelViewController and assign numberString to that property.
In LabelViewController assign the property to the label in viewDidLoad.
In TableViewcontroller
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.rowNumber = indexPath.row;
self.numberString = [NSString stringWithFormat:#"%lu", self.rowNumber];
LabelViewController *labelViewController = [[LabelViewController alloc] init];
NSLog(#"The row number is %#", self.numberString);
labelViewController.labelstring = self.numberString;
[self.navigationController pushViewController:labelViewController animated:YES];
}
In LabelViewController
.h File
#property(nonatomic,strong)NSString * labelstring;
.m File
- (void)viewDidLoad {
[super viewDidLoad];
self.rowNumberLabel.text = labelstring;
}
In my ios app I have created one NSModel Object for displaying tableList and when I tapped on tableList row I want to take that particular model class object data to another class
How can I do this?
my code:
NSMutableArray * mainArray;
//TableList Delegate Methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return mainArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"HistoryCell";
// Similar to UITableViewCell, but
UITableViewCell *cell = (UITableViewCell *)[MaintableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
model1 = mainArray[indexPath.row];
cell.textLabel.text = model1.Name;
cell.detailTextLabel.text = model1.MasterId;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController1 *Controller = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
Controller.indexPosition = indexPath.row;
[self.navigationController pushViewController:Controller animated:YES];
}
#end
ModelObject1:
#import "ModelObject1.h"
#implementation ModelObject1
#synthesize MasterId,Name;
-(void)loadingservices :(id)mainDictionary{
NSLog(#"loadingservices %#",mainDictionary);
Name = [mainDictionary objectForKey:#"Name"];
MasterId = [mainDictionary objectForKey:#"MasterId"];
}
#end
Create one model object in your Destination View controller
#property (strong,nonatomic) ModelClass *job;
in your did select method get that model data and pass it
ModelClass *SelectedJob;
SelectedJob = [muarrJobs objectAtIndex:indexPath.row];
JobDescriptionViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:#"JobDescription"];
obj.job = SelectedJob;
[self.navigationController pushViewController:obj animated:YES];
Use this Code,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController1 *Controller = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
model1 = mainArray[indexPath.row];
Controller.modelVal = model1;
[self.navigationController pushViewController:Controller animated:YES];
}
Another ViewControllerB:
ViewControllerB.h
declare the property Value
#property (strong, nonatomic)modelName *modelVal;
ViewControllerB.m
print the name value in viewDidLoad:
NSLog(#"%#",modelVal.Name);
hope its helpful
Try like below:
1) Create one model object in your ViewController1
#property (strong,nonatomic) ModelObject1 *modelObj;
2) Write below code in viewDidLoad of ViewController1,
modelObj = [[ModelObject1 alloc]init];
Then Replace your didSelect method like this,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController1 *Controller = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
Controller.indexPosition = indexPath.row;
ModelObject1 *model = mainArray[indexPath.row];
Controller.modelObj = model;
[self.navigationController pushViewController:Controller animated:YES];
}
Then get you data from model class in viewDidLoad of ViewController1
e.g, NSLog("%#", modelObj.Name);
This way you can pass model class object to other view controller. Hope this will help you.
Right now I have a kind of pop-up selector so that the user can pick an option on my screen. Please see this sample project to see what I mean.
I'm using the same method as this sample, except that I have three different buttons where the user will select. My problem is that I am using this code:
- (void)itemSelectedatRow:(NSInteger)row
{
NSLog(#"row %lu selected", (unsigned long)row);
[self.selectSeverity setTitle:[self.severityArray objectAtIndex:row] forState:UIControlStateNormal];
[self.selectStatus setTitle:[self.statusArray objectAtIndex:row] forState:UIControlStateNormal];
[self.selectGender setTitle:[self.genderArray objectAtIndex:row] forState:UIControlStateNormal];
}
...and it is changing the name of the button for all three every time the user selects one. So for example, if the user taps the "selectSeverity" button, and chooses the item on row three, the name for the selectStatus and selectGender button will also change to the item on row three on its own corresponding array.
What I need to do is somehow separate this method so the button's title changes only when a row has been selected in its own array: how can I do this?
More information:
I have these tableViews embedded in a Navigation Controller:
statusViewController.m/.h
genderViewController.m/.h
pickerViewController.m/.h (this corresponds to the Severity button)
Each have a delegate with a separate ID, but the same content:
#protocol correspondingViewControllerDelegateHere <NSObject>
#required
- (void)itemSelectedatRow:(NSInteger)row;
#end
#interface correspondingViewControllerHere : UITableViewController
#property (strong, nonatomic) NSArray *correspondingArrayHere;
#property (assign, nonatomic) id<statusViewControllerDelegate> delegateIdHere;
#end
Each have the same content in the .m file as well, but correspond to their own delegates, arrays, etc.
#implementation statusViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statusData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CellIdentifierHere";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.statusData objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([self.delegateIdHere respondsToSelector:#selector(itemSelectedatRow:)]) {
[self.delegateIdHere itemSelectedatRow:indexPath.row];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
#end
In manualViewController.h (this is the view where they are implemented) I have declared the delegates.
Lastly, in the file manualViewController.m, I have the following code to implement them:
- (IBAction)showinfo:(id)sender
{
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:#"pickerVC"];
pickerViewController *tableViewController = (pickerViewController *)[[navigationController viewControllers] objectAtIndex:0];
tableViewController.severityData = self.severityArray;
tableViewController.navigationItem.title = #"Triage Severity Levels";
tableViewController.delegate1 = self;
[self presentViewController:navigationController animated:YES completion:nil];
}
- (IBAction)showStatus:(id)sender {
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:#"statusVC"];
statusViewController *tableViewController = (statusViewController *)[[navigationController viewControllers] objectAtIndex:0];
tableViewController.statusData = self.statusArray;
tableViewController.navigationItem.title = #"Triage Severity Levels";
tableViewController.delegate3 = self;
[self presentViewController:navigationController animated:YES completion:nil];
}
- (IBAction)showGender:(id)sender {
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:#"genderVC"];
genderViewController *tableViewController = (genderViewController * [[navigationController viewControllers] objectAtIndex:0];
tableViewController.genderData = self.genderArray;
tableViewController.navigationItem.title = #"Triage Severity Levels";
tableViewController.delegate2 = self;
[self presentViewController:navigationController animated:YES completion:nil];
}
SO as I said before, I think the code lies in the itemSelectedAtRow. Can someone help me separate this method so that the choice of one item does not affect the choice of another item?
Thanks
One solution is to change the protocol to require three different methods
#protocol correspondingViewControllerDelegateHere <NSObject>
#required
- (void)itemSelectedSeverityAtRow:(NSInteger)row;
- (void)itemSelectedStatusAtRow:(NSInteger)row;
- (void)itemSelectedGenderAtRow:(NSInteger)row;
#end
Then implement the three methods in the manualViewController and call the appropriate method from each of the other view controllers.
I have a UITableView which has different types of files and folders in it, Right i have set a method that passes to another view controller once clicked on a row. What i need is that once a row is clicked on it checks what kind of a file is in the row and then connects to different uiviewcontrollers on the basis of that.
My UiTableView has two items in each cell
A Cell Label & Cell Detail Text Label
The DetailTextLabel holds the Subject type
i.e. Folder (For Folders) & File (For Files like jpeg. , png., etc.)
I want to use the if condition in the didselectrowatindexpath to distinguish between file and folder
You can do that by checking value of cell.detailTextLabel.text like following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.detailTextLabel.text;
if ([str isEqualToString:#"Folder"])
{
// Open Folder Detail View. For Example:
FolderViewController* objVC = [[FolderViewController alloc] initWithNibName:#"FolderViewController" bundle:nil];
[self.navigationController pushViewController:objVC animated:YES];
}
else if ([str isEqualToString:#"File"])
{
// Open File Detail View. For Example:
FileViewController* objVC = [[FileViewController alloc] initWithNibName:#"FileViewController" bundle:nil];
[self.navigationController pushViewController:objVC animated:YES];
}
}
in .h file
#import "FolderViewController.h"
#import "FileViewController.h"
#interface mainViewController : UIViewController {
FolderViewController *folderViewObj;
FileViewController *fileViewObj;
}
in .m file
- (void)viewDidLoad {
[super viewDidLoad];
folderViewObj = [[FolderViewController alloc] initWithNibName:#"FolderViewController" bundle:nil];
fileViewObj = [[FileViewController alloc] initWithNibName:#"FileViewController" bundle:nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tblObj cellForRowAtIndexPath:indexPath];
NSString *lblText = cell.DetailTextLabel.text;
if ([lblText isEqualToString:#"Folder"]) {
[self.navigationController pushViewController:folderViewObj animated:YES];
}
else if ([lblText isEqualToString:#"File"])
{
[self.navigationController pushViewController:fileViewObj animated:YES];
}
}
-(void) dealloc {
[folderViewObj release];
[fileViewObj release];
[super dealloc];
}
using this way, object of FolderViewController and FileViewController is created only once not all time when user can select the row of uitableview.
I followed the SimpleDrillDown app example in the docs for a workout app that shows workout names in the first UITableView and exercises in the second UITableView.
My app is in Dropbox here: http://db.tt/V0EhVcAG
I used storyboards, have prototype cells, but when I load in simulator the first UITableView won't let me click and go to the detail UITableView. The disclosure indicator chevrons don't load. The app builds successfully and there are no formal errors.
My tableviews are in a navigation controller, my segue and prototype cells are all named accordingly in storyboard.
SpitfireViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataController countOfList];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"WorkoutCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Workout *workoutAtIndex = [dataController objectInListAtIndex:indexPath.row];
cell.textLabel.text = workoutAtIndex.title;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showExercises"]) {
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.workout = [dataController objectInListAtIndex:selectedRowIndex.row];
}
}
DetailViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [workout.exercises count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ExerciseCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [workout.exercises objectAtIndex:indexPath.row];
return cell;
}
AppDelegate.h
#import <UIKit/UIKit.h>
#class DataController;
#class SpitfireViewController;
#interface SpitfireAppDelegate : UIResponder <UIApplicationDelegate>
{
UIWindow *window;
SpitfireViewController *spitfireViewController;
DataController *dataController;
}
#property (strong, nonatomic) UIWindow *window;
#end
AppDelegate.m
#import "SpitfireAppDelegate.h"
#import "SpitfireViewController.h"
#import "DataController.h"
#implementation SpitfireAppDelegate
#synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
spitfireViewController = [[SpitfireViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:spitfireViewController];
DataController *controller = [[DataController alloc] init];
spitfireViewController.dataController = controller;
[window addSubview:[navController view]];
[self.window makeKeyAndVisible];
return YES;
}
#end
If you have tableview cell segue selection issue.
You need to check first that your tableview allows selection
You do not need to implement UITableViewDelegate and UITableViewDataSource when using "Static cells"
You need to add a segue for the accessory action + a segue for the selection.
When using Storyboards, you'll typically set the storyboard file name in the Project settings under the info tab. Once you've selected your storyboard there, you can basically delete everything except the return YES part of the application:didFinishLaunchingWithOptions: method. It's all taken care of for you in the storyboard.
EDIT:
Here's where you set the storyboard:
Also make sure that your view controller is set as the initial view controller:
I think this is your problem:
spitfireViewController = [[SpitfireViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:spitfireViewController];
DataController *controller = [[DataController alloc] init];
spitfireViewController.dataController = controller;
The spitfireViewController you create here is not the one in your storyboard, it's a new one. You should delete all this code, as you already have a spitfireViewController embedded in a navigation controller that you made in the storyboard. You should set the data controller for spitfireViewController in it's viewDidLoad method:
DataController *controller = [[DataController alloc] init];
self.dataController = controller;
Where is your "didSelectRowAtIndexPath" method?
I would recommend putting that in and then triggering [self performSegueWithIdentifier:] to trigger the segue using the data in the table cell as the sender.