How to display value of selected row to next view controller? - ios

I have an array.This array loading from web service in TableView.
There are all BranchId in tableview.I want display fields of selected branchId when selected row.
e.g Selected "1234" in tableview
Open new view controller(DetailViewController) :
BranchID:1234
BranchName: ABCDEFGH
I have Branchname in web service
TableviewCodes: http://pastie.org/8052416
How can I display selected ID's detail on new view controller ? Thanks

There are different ways, here is one:
From your first viewController:
NSDictionary* dict = [NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:theIdYouWantToSend]
forKey:#"index"];
[[NSNotificationCenter defaultCenter] postNotificationName: #"getID" object: dict];
Now from the new view controller (detailViewController), in the viewDiDLoad method:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(getID:) name:#"getID" object:nil];
and create method:
-(void)getID:(NSNotification*)notification{
NSDictionary* dict = (NSDictionary*) notification.object;
}
You can easily get the ID from the dictionary
myId = [dict objectForKey:#"index"];

you should modify didSelectRow method by below way:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *second=[[DetailViewController alloc]
initWithNibName:#"DetailViewController" bundle:nil] ;
second.branchId = [myArray objectAtIndex:indexPath.row];
[self presentModalViewController:second animated:YES];
}

Related

Wrong object added to UITableView

In my app I get an object by NSNotificationCenter (form another controller) and add the object to UITableView:
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(RosterSave:) name:#"RosterSave" object:nil];
}
-(void)RosterSave:(NSNotification *)notification
{
NewRoster* newRoster = [[NewRoster alloc]init];
newRoster = notification.object;
[myUser.rosterArray addObject:newRoster];
[self.myRoster reloadData];
}
This is the tableView method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return myUser.rosterArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *iden = #"MyTable";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
}
NewRoster* myNewRoster = [myUser.rosterArray objectAtIndex:indexPath.row];
cell.textLabel.text = myNewRoster.nameRoster;
return cell;
}
When the user adds the first object, the tableView get own row. When the user adds the second object, it adds two rows of the second object and on this way.
How can I fix this issue?
You have add observer(notification) in viewWillAppear which get called everytime when view will appear.
add notification in viewDidLoad instead of viewwillAppear.
I always like to put NSNotification subscriptions in init / and unsubscriptions in dealloc. This pattern is easy to read and debug. Also, it guarantees you will never double subscribe or double unsubscribe.
In your case, you are prone to creating multiple subscribtions in viewWillAppear
- (instancetype)init
{
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(RosterSave:) name:#"RosterSave" object:nil];
...
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#Feroz is right about you allocating a new object and replacing it with notification.object. #Lion is right about viewDidLoad vs. viewDidAppear You are generating multiple notifications. You need to only generate one per object. Put a breakpoint in your RosterSave code and count how many times it's called per new object. Also look at the stack trace to see who is generating these calls. It's down to a simple matter of stepping through, understanding your code, and seeing what's happening.

Pass Date Picker date back to View Controller to show Date in label

I've researched a bunch of questions on how to do this, and am coming up just short.
I have ViewControllerA and ViewControllerB.
ViewControllerB is passing the NSDate from the UIDatePicker back to ViewControllerA.
I'm fine until trying to put that NSDate as a label in the TableViewCell it corresponds with.
Can you help? Thanks!
ViewControllerA
- (void)addItemViewController:(EXEDurationPickerViewController *)controller didFinishEnteringItem:(NSString *)item {
NSLog(#"This was returned from ViewControllerB %#", item);
}
item is the Date picked from ViewControllerB. How do I get it to show up as a label in the corresponding TableViewCell?
Use delegate to pass the date or other option is send Notificaition
Add this in ViewControllerA
#interface ViewControllerA : UIViewController{
NSIndexPath *selectedIndexPath;
}
#end
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveNotification:)
name:#"dateSend"
object:nil];
}
- (void) receiveNotification:(NSNotification *) notification
{
NSString *item = notification.userInfo[#"date"];
// show for what cell you want to show
//keep selectedIndexPath as instance Variable
YourCell *cell = (YourCell *)[self.tableView cellForRowAtIndexPath:selectedIndexPath];
cell.label.text = item;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndexPath = indexPath;
}
//Post the notification fro `ViewControllerB`
- (void)addItemViewController:(EXEDurationPickerViewController *)controller didFinishEnteringItem:(NSString *)item {
NSLog(#"This was returned from ViewControllerB %#", item);
[[NSNotificationCenter defaultCenter] postNotificationName: #"TestNotification" object:nil userInfo:#{#"date":item}];
}
In the didSelectRowAtIndexPath (or in prepareForSegue if you're using that instead) save the indexPath of the selected cell in a property. Then, in your delegate method, add item to your model (whatever you're populating your table view with), and then call reloadRowsAtIndexPath: with that saved indexPath to update the table.

NSNotification not returning

I have a modalUIViewController that has a UITableView on it. For whatever cell the user selects, I want to return that text to the previous view controller and dismiss the modal view. I'm using NSNotifications to send the value back. Problem is, my notification is never received.
Here is the code from the 'parent' view:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(choiceReceived:)
name:#"selectionMade"
object:nil];
[self performSegueWithIdentifier: #"locationsDetailsSegue" sender: self];
}
- (void) choiceReceived: (NSNotification *) notification
{
NSLog(#"test");
NSDictionary *dict = [notification userInfo];
NSString *user_choice = [dict objectForKey:#"choice"];
NSLog(#"%#", user_choice);
[[NSNotificationCenter defaultCenter] removeObserver:self
name: #"selectionMade"
object:nil];
}
And in the modal view controller:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSString *choice = cell.textLabel.text;
// send a notification of this choice back to the 'parent' controller
NSDictionary *dict = [NSDictionary dictionaryWithObject:choice forKey:#"choice"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"selectionMade" object:nil userInfo:dict];
NSLog(#"%#", [dict objectForKey:#"choice"]);
[self dismissViewControllerAnimated:YES completion:nil];
}
I get the correct output from the notifier, but I get no output whatsoever from the receiver. Am I missing something obvious? Thanks!
Well, i don't like use NSNotificationCenter in such scenario (Its just my suggestion). I'm always recommend delegate pattern in such case. Delegation pattern working or communicate one-to-one object notification so it give 100% precise output and removing other conflicts.
Create protocol methods in childviewcontroller and delegate property for confirmation in parentclassviewcontroller.
Consume chileviewcontroller protocol in parentviewcontroller. Implement required delegate methods of protocol in parentviewcontroller class. Also you can send multiple types of arguments through delegates method.
for more info go through this doc.

How to send some value to another view controller when i select cell?

I have two View Controllers: MainScreenViewController and TargetScreenViewController;
In MainScreenViewController I have UITableView with custom cells, in TargetScreenViewController I have the label which i need change.
I need to get some value, index path for example, and pass it along to TargetScreenViewController and change the label based on that value.
I already setup the push segue in Triggered Segues in cell, but how can i pass some value and get it there?
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TargetView *target = [[TargetView alloc]initWithNibName:#"TargetView" bundle:nil];
NSString *data = #"Success";
target.incomingData = data;
}
in TargetView:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mLabel.text = self.incomingData;
}
but label is blank =[
try this .....
1.create a property in your target view controller .
2.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
targetviewcontroller * obj = create object here .
obj.property = dataToBeSent ;
---------------
}
then you will get your data by accessing property
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Class *yourObject=[self.arr objectAtIndex:indexPath.row];
TargetView *vc=[[TargetView alloc] initWithNibName:#"TargetView" bundle:nil];
vc.object=yourObject; //Create a p
[self.navigationController pushViewController:vc animated:YES];
}
In your TargetView
-(void)viewDidLoad{
self.lbl.text=self.object.value;
}
You can make a notification and post notification with a object. The object what you need to send to second Controller.

Updating a property in Core Data in a View Controller IOS

I am making an Iphone App and I want to change one of the values in Core Data. Normally I have only done the didselect row at index path method in a table view. Now I am coming from a table view and going into one of the cells and want to make a change for one of the properties in that cell in the viewController.
Doing this : NSManagedObject *d = [self.model.frc_Work objectAtIndex:1]; will give me an error.
//The way I have done it before
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *d = [self.model.frc_Work objectAtIndexPath:indexPath];
[d setValue:[NSNumber numberWithInt:1] forKey:#"status"];
}
//How do I do it this way
- (IBAction)btnDone:(id)sender {
NSManagedObject *d = [self.model.frc_Work objectAtIndex:1];
[d setValue:self.tvTheNote.text forKey:#"notes"];
[self.model saveChanges];
self.tvTheNote.text = #"";
[self dismissModalViewControllerAnimated:YES];
}
My Frc Code is :
- (NSFetchedResultsController *)frc_Work
{
// If the frc is already configured, simply return it
if (_frc_Work) return _frc_Work;
// Otherwise, create a new frc, and set it as the property (and return it below)
_frc_Work = [_cdStack frcWithEntityNamed:#"Work" withPredicateFormat:nil predicateObject:nil sortDescriptors:#"title,YES" andSectionNameKeyPath:nil];
return _frc_Work;
}
Create an index path using...
NSIndexPath rowIndexPath = [NSIndexPath indexPathWithIndex:1];
Then call:
NSManagedObject *d = [self.model.frc_Work objectAtIndexPath:rowIndexPath];

Resources