Modify uilabel from a view to other - iphone sdk - xcode

I have TableViewController & DetailViewController.
I'd like to modify one label named 'label' in DetailViewController.
TableViewController.h :
#interface CodexTableView : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *listOfItems;
DetailViewController *vc;
}
#property (nonatomic, retain) DetailViewController *vc;
#property (strong, nonatomic) IBOutlet UITableView *myTable;
#end
And an extract of my TableViewController.m :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
vc.label.text = #"bonjour";
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//NSLog(#"%#",cell.textLabel.text);
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
dvController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
But it doesn't works.
Someone know the error ?
Please help me.
Thanks :)

You mix up two variables for your detail view controller. Remove the dvController one and only work with your vc property, like so:
vc = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[[vc label] setText:#"bonjour"];
Make label a property of your detail view controller and have it point at your onscreen label.

Related

Compilation fails due to prototype cell

I have a TableViewController subclass with one prototype cell designed in storyboard. Then I am passing the references to the class header etc and for some reason it fails to compile it. But the connections seem fine. I provide you the code and pictures of the build errors and the storyboard. I am using Xcode 4.3.3. Any help is really appreciated.
favTable.h
#import <UIKit/UIKit.h>
#interface favTable : UITableViewController <NSFetchedResultsControllerDelegate>
{
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
NSArray *favArr;
NSMutableArray *favName;
NSMutableArray *favScore;
}
#property (nonatomic, retain) NSArray *favArr;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, strong) NSMutableArray *favName;
#property (nonatomic, strong) NSMutableArray *favScore;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#property (strong, nonatomic) IBOutlet UITableViewCell *celldes;
#property (strong, nonatomic) IBOutlet UIImageView *cellimage;
#property (strong, nonatomic) IBOutlet UILabel *cellname;
#property (strong, nonatomic) IBOutlet UILabel *cellmanu;
#property (strong, nonatomic) IBOutlet UILabel *cellscore;
#end
favTable.m
#import "favTable.h"
#import "ecoAppDelegate.h"
#interface favTable ()
#end
#implementation favTable
#synthesize favArr;
#synthesize managedObjectContext;
#synthesize fetchedResultsController;
#synthesize favName;
#synthesize favScore;
#synthesize celldes;
#synthesize cellimage;
#synthesize cellname;
#synthesize cellmanu;
#synthesize cellscore;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Favorites";
self.navigationController.navigationBar.translucent = NO;
// passing the array of addedtofavorites to the total one with all favorites
self.managedObjectContext = ((ecoAppDelegate *) [UIApplication sharedApplication].delegate).managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"FavoritesInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
fetchRequest.resultType = NSDictionaryResultType;
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:#"name", nil]];
NSError *error=nil;
self.favArr=[[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]mutableCopy];
if (error!=nil) {
NSLog(#" fetchError=%#,details=%#",error,error.userInfo);
}
self.favName = [[self.favArr valueForKey:#"name"] mutableCopy];
self.favScore = [[self.favArr valueForKey:#"score"] mutableCopy];
}
- (void)viewDidUnload
{
[self setCelldes:nil];
[self setCellimage:nil];
[self setCellname:nil];
[self setCellmanu:nil];
[self setCellscore:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [favName count];;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
// Configure the cell...
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cellname.text = #"Test";
return cell;
}
#end
I moved manually the IB outlets into a separate custom cell class and then linked the references to the storyboard (maybe this is fixed in newer versions)
Then applied that cell style in the prototype cell and simply changed the content of the UI items of my cell using the identifier (no tag was needed)

xCode 4.5 Navigation Controller and back button how to save values

In my code I created this
#import <UIKit/UIKit.h>
#interface ViewControllerTable : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (strong, nonatomic) NSString *plcVar;
#end
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
listaPlacas *placas = [[listaPlacas alloc]init];
[self.navigationController pushViewController:placas animated:YES];
}
But in the other view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for (UITableViewCell *cell in [tableView visibleCells]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *placaVar = cell.textLabel.text;
ViewControllerTable *VCT = [[ViewControllerTable alloc]init];
VCT.plcVar = [[NSString alloc]init];
VCT.plcVar = placaVar;
}
When I click in back button the value in variable plcVar is empty.
I assume that "other view" is the viewController you push to from your first ViewController, and you are trying to set a property in the firstViewController from your otherViewController.
Then here is where you go wrong:
ViewControllerTable *VCT = [[ViewControllerTable alloc]init];
You are creating a new instance of VCT, when in fact you want a reference to the VCT that you pushed from.
You can get this via your navigation controller...
NSArray* viewControllers = [self.navigationController viewControllers];
ViewControllerTable* VCT = [viewControllers objectAtIndex:
[ viewControllers count]-2];

Using and updating a NSMutableString in AppDelegate for two viewcontrollers

I have a NSMutableString defined in my AppDelegate.h file.
//AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
#public
NSMutableString *nidForDetailDisplay;
}
Then I set this string in my ViewController.m file. I am storing a string (nidForDetailDisplay) to pass from a tableview to detailview.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
rowNumber = indexPath.row;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// this is where I set the string to a string of text
appDelegate->theNidForDetailDisplay = [arrayOfNids objectAtIndex:rowNumber];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIViewController *detailViewController;
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
[self.navigationController pushViewController:detail animated:YES];
}
In ViewController, when I log theNidForDetailDisplay, I get the correct string.
Next, I try to access this updated NSMutableString (nidForDetailDisplay) in the subsequent DetailViewController
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableString *storyid;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate->theNidOnDetailDisplay = storyid;
}
However, when I print the string (storyid) or (theNidOnDetailDisplay) they both return null.
Am I missing something here? Should I be using instance variables?
Thanks in advance.
Don't use your AppDelegate to pass data. Add a property to the detail view controller and assign the value to the property.
on DetailViewController:
#property (nonatomic) NSMutableString * nid;
The assign it before you present:
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
detail.nid = [arrayOfNids objectAtIndex:rowNumber];
[self.navigationController pushViewController:detail animated:YES];

cell.detailTextLabel not working

I want to set up a textLabel and a detailTextLabel for my table. The textLabel is working properly. However, I couldnt get the detailTextLabel to display the text that I have set. Below is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"SomeString";
cell.detailTextLabel.text = #"hello";
return cell;
}
I have faced similar issue as like #user1829700.
My initial settings are
TableView --> Prototype Cell --> (Attribute Inspector) Style - set to
Custom (by design)
Changed Style to - Subtitle, it does the trick
Try this
Use appropriate UITableViewCellStyle with this (anything but UITableViewCellStyleDefault should thus work). The cell's style is specified when you initialize it.
In your custom table cell implementation try adding #synthesize detailTextLabel;
Header (.h):
#property (nonatomic, weak) IBOutlet UILabel *textLabel;
#property (nonatomic, weak) IBOutlet UILabel *detailTextLabel;
Implementation (.m):
#synthesize textLabel;
#synthesize detailTextLabel;

Multiple Views for UITabBarController

I'm trying to build an app where I have a TabBarController with 4 entries.
When I select the first entry, a view with a UITableView shows up.
This TableView is filled with several entries.
What I would like to do is:
When an entry out of that UITableView gets selected, another view should show up; a detailview.
.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
if(self.secondView == nil) {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:[NSBundle mainBundle]];
self.secondView = secondViewController;
[secondViewController release];
}
// Setup the animation
[self.navigationController pushViewController:self.secondView animated:YES];
}
}
.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#interface FirstViewController : UIViewController {
SecondViewController *secondView;
NSMutableArray *myData;
}
#property (nonatomic, retain) SecondViewController *secondView;
#property (nonatomic, copy, readwrite) NSMutableArray* myData;
#end
This is what I have so far.
Unfortunately.. the code runs, bit the second view does not show up.
Is your first view controller wrapped in a UINavigationController? When you set up your UITabBarController, you should add UINavigationControllers rather than your UIViewController subclasses, e.g.:
FirstViewController *viewControl1 = [[FirstViewController alloc] init];
UINavigationController *navControl1 = [[UINavigationController alloc] initWithRootViewController:viewControl1];
UITabBarController *tabControl = [[UITabBarController alloc] init];
tabControl.viewControllers = [NSArray arrayWithObjects:navControl1, <etc>, nil];
//release everything except tabControl
Also, based on your code, you don't need to keep your secondViewController as an ivar, since UINavigationController automatically retains its view controllers (and retaining it when you're not displaying it will use up unnecessary memory).

Resources