How to change the Images in table view which is lied in the new view controller based on row selected .
e.g. Say I hv category table in my Category view controller.Now when user taps on Camera,a new view controller should load containing all the camera images and on tap of any image,it should display its Detail.
how to implement this , as I am new to this tech.,Please help me ...
you can follow some simple steps to make it working.
1. First View: add the following code in your didSelectRowAtIndexPath method of tableview
-(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Fetch the value of the selected row in NSString
NSString *tableRowValue = cell.textLable.text;
//Call the next view
secondView *secondViewController = [[secondVIew alloc] initWithNibName:#"secondView" bundle:nil];
secondViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve ;
//Initialize the view with the clicked row value thwough NSString variable
[secondViewController initializeWith:tableRowValue];
}
2.Second View:
In your .h file, add the following initialization method:
-(void)initializeWith:(NSString *)sentRowValue;
IN your .m file, implement the initilization method:
-(void)initializeWith:(NSString*)sentRowValue
{
NSString *receivedRowValue = sentRowValue;
//take log to check
}
// now perform all the table view delegate methods using the value you have received.
NOTE: declare all the variables in your .h file and make them as property and synthesize them in your .m file
hope this helps
Related
I'm having trouble transferring data between a table view controller and another view controller.
On my sending controller - tableviewcontroller.m - I have this under prepareForSegue:
FriendDetailViewController *fvc = (FriendDetailViewController *)segue.destinationViewController;
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
PFUser *u = self.friends[path.row];
fvc.nameLabel.text = u.username;
//friendViewController.m (the destination view controller)
- (void)viewWillLoad {
[super viewDidLoad];
if (self.user) {
self.nameLabel.text = self.user.username;
}else{
self.nameLabel.text = #"No data to display";
}
}
The label displays "No data to display".
I've read other posts and viewed tutorials but I cannot figure this out.
You're only setting the nameLabel in your prepareForSegue, not the actual user, so it is uninstantiated when your new VC loads.
Try adding this into your prepareForSegue:
fvc.user = u;
Assuming your user you want to pass is stored in u, that should do it (also assuming user is a public property of the new view controller.)
You can put an attribute in your .h file of FriendDetailViewController and the set it before calling:
fvc.attribute = u.username;
After this, you can call pushViewController for "fvc" inside didSelectRowAtIndexPath
You can't set the UI actions on prepareForSegue.
You could instead define the user variable on destination viewcontroller.
After that, on prepareForSegue, you can set the selected PFUser to the destination viewcontroller's user.
And when you'd like to show the user's name, use viewWillAppear instead of viewWillLoad.
If you can't understand, please let me know, I can show detailed code for all.
I am currently working on a project that requires a list of customers to be displayed in a UITableView, the associated cell then segues to a TabView to display a detailed customer record in a tabbed ui.
I have setup the story board with the required TableView and populated fine. The TabViews all setup and I have added a custom class to the main TabView controller which can take the ID (required to interrogate service and return further data) and Customer Name. I have also added a UIViewController for the first tab in which I need to get the ID value.
I can't seem to get hold of the ID or Company Name that is passed. I have tried importing the .h file of the UITabView. I know the UITabView .h file is being populated with the values as in the .m file I am using the Customer Name to update the title of the Navigation Bar. However, whenever I breakpoint on line that gets the ID in the .m file for the individual tab, it always returns nil.
I am using the following code to try and get this value:
companyTabController *headerData = [companyTabController alloc];
_companyName_lbl.text = headerData.companyName;
_companyID_lbl.text = headerData.ID;
I have tried several variations of the above and all to no avail.
You can also use NSUserDefaults to save the data, I think that is the simplest way to save the data throughout the app.
From the code you posted, the headerData is a new instance. So the companyName and the ID will be nil unless you assign some value to them.
Since, you mentioned that you are able update the navigation bar title, try using the same object for fetching the values in this controller as well. (Maybe you can use a singleton object)
If your segueing you have to use the prepareForSegue:sender: method as such:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
companyTabController *companyTC = [segue destinationViewController];
companyTC.companyName_lbl.text = headerData.companyName;
etc
}
if your not segueing you will have to instantiate it as such :
- (void) didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
companyTabController *companyTC = [self.storyboard instantiateViewControllerWithIdentifier:#"CopmanyTabController"];
companyTC.companyName_lbl.text = selectedCell.textLabel.text or = headerData.companyName;
[self.navigationController pushViewController:companyTC animated:YES];
}
I'm new to objective C and design patterns like MVC, protocols and so on but this is it:
I am trying to write an iOS app within two viewcontrollers: the first has a textview where the user can write into, and the second has a UISwitch that triggers on "Value changed" and saves a file.
If I toggle by hand the switch on the SecondViewController it will save the file and that's ok.
But I wish the file could be saved from the FirstView just when the user types a specific word, it auto-switches to the second view, and auto-activates the UIswitch and all the method already behind it.
I still can't get the two interfaces working this way. Thanks everybody in advance for helping. Cheers!
this is connected in SecondViewController.h in the storyboard
-(IBAction)toggleFileSave:(id)sender;
and it is implemented as usual...
#interface SecondViewController ()
#property (nonatomic,weak) IBOutlet UISwitch *mySaveFileSwitch;
#end
- (void) toggleFileSave:(id)sender {
// how do I execute this code when the user
// type a specific word in the first view??
}
Create a BOOL flag in your SecondViewController.
Set it when the specific word is typed and push the view controller.
In the viewDidLoad of SecondViewController check the flag condition.If it is set call the required method.
When the specific word is typed:
ViewController2 *viewController = [ViewController2 alloc]init];
viewController2.flag = YES;
[self.navigationController pushViewController:viewController2 animated:YES];
In your text field delegate (add one if it doesn't exist) add this method:
- (void)textFieldDidEndEditing:(UITextField *)textField {
/* at this point the user finished editing */
NSString *currentText = /* read text field value */
if ([currentText isEqualToString:/* the magic word */]) {
/* save the file, present a view controller, etc. */
}
}
Check UITextFieldDelegate to know the available methods, you may need more than one to get the desired behaviour.
If you want to load the second view controller in order to show the UI and the save the file you can do as サンディープ said in his or her answer:
SecondViewController *controller = [SecondViewController new]; /* init as usual */
controller.saveOnLoad = YES;
[self.navigationController pushViewController:controller animated:YES];
Then, in SecondViewController:
- (void)viewDidLoad {
if (self.saveOnLoad) {
/* save file in async block */
/* set switch on */
}
}
If you don't need to show the second view I'd move the saving functionality to its own class and use it from the first controller, showing just a confirmation message for instance.
I'm struggling to get my data from a UIContainerView to another UIContainerView. For example: I have 2 containers, which are calculatorContainer and displayResultContainer. So when I press "=" button to calculate the result, I want it to show up in the displayResultContainer. I already tried different options with the segue method and parentViewController access, but still no luck.
There are two possibilities.
1.Using appDelegate. Use a property in app delegate to pass data between containers.
Put this in first container
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
appDelegate.dataToPass=dataToPass;
in the second container
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
dataToPass=appDelegate.dataToPass;
2.Using ParentViewController.
in first container
ParentViewController parent=(ParentViewController *)[self parentViewController];
parent.dataToPass=dataToPass;
in the second container
ParentViewController parent=(ParentViewController *)[self parentViewController];
data=parent.dataToPass;
Use delegates.
Steps:
From 1st UIContainerView, call a delegate method to the parent view controller.
The parentview controller then pass the value to the 2nd UIContainerView.
You have to declare this method in the second UIContainerView:
UIContainerView2
-(id)initWithsetresult:(Int)Result {
int showPreResult = result;
return self;
}
On button action:
antagonist = [[UIContainerView2 alloc]initWithsetresult: calculateResult];
Here is an easy method, but first you should know that this method works for forward flow not backward (in backward flow you will need a thing called delegate):
View 1 = calculator
View 2 = result display.
In view2 get a text field and an NSString in its .h file:
#property(nonatomic,strong) NSString *myResult; //to contain the result
#property(nonatomic,strong) IBOutlet UITextfield *myResultText; // to display the result
In the first View (view1) take inputs and add buttons like (addition +) etc.:
#property(nonatomic,strong) IBOutlet UIButton *addition;
Connect that Outlet(button) with the view controller. Then add a method for that button and connect it to that button:
-(int)addFunc;
Then take in user input from 2 text fields that you have already made in the 1st View (input1TextField and input2TextField):
Inside the addFunc write:
(int)addFunc{
int input1,input2,result;
input1 = self.input1TextField.text;
input2 = self.input2TextField.text;
result = input1+input2;
return result;
}
Now inside the IBaction method write:
-(IBAction)myIbaction{
[self performSegueWithIdentifier #"seguename", sender :self]; // set the segue name and fill it here.
SecondViewController * sec = [segue destinationViewController];
int result = [self addFunc];
sec.myResult = (NSString)result;
}
Now in the second View (view2) in viewDidLoad method write:
-(void)viewDidLoad{
self.myResultText = self.myResult;
}
And it will display your result.
Don't know if it helps or not but surely you get the picture that this is how you will be able to perform it.
Hope it helps.
So, I have one ViewController that has a TableView populate with an Array (_vinhoArray) and another TableViewController that opens when a user taps a row in this first ViewController.
What I want is set the title of second View ( TableViewController) for the name of row selected.
In this first one I have this code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedRow = [_vinhoArray objectAtIndex:indexPath.row];
// SubTable is the name of Second View
SubTable *subTable = [[SubTable alloc] init];
subTable.titleView = selectedRow;
}
In the second view a have a property set
#property (nonatomic, strong) NSString *titleView;
In the -(void)viewDidLoad I have this code
self.title = titleView;
But nothing shows in the title of the Second View (TableViewController)
Thats it! Please Help
If you simply want to set the title on the navigation bar of the view controller you are presenting, there's no need to create a property for it. You probably want to do something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *titleString = [_vinhoArray objectAtIndex:indexPath.row];
// SubTable is the name of Second View
SubTable *subTable = [[SubTable alloc] init];
subTable.title = titleString;
[self.navigationController pushViewController:subTable animated:YES];
}
Above seems to be the easiest and most straight-forward way to implement this. On a side note, it's pretty unconventional to name a variable type like an NSString with some other class type, like titleView or titleLabel. Something similar to "titleString", like I used, would be more appropriate and less confusing for anyone reading your code.
What is probably happening is that the view is loading before you set the titleView property. As you are setting the title statically in viewDidLoad, no changes are being shown. You can confirm this by putting in a couple of breakpoints and seeing the order of the calls being made.
One solution to this is to use a more dynamic method of setting the table. Create a custom accessor in your second view controller:
- (void)setTitleView:(NSString *)title {
_titleView = title; // This sets the backing iVar
self.title = titleView;
}
Now, whenever you set the property, then the change is applied, even if it is after the view has loaded.
You don't need to declare the method or the iVar, these are set up by autosynthesis; you are merely overriding the default implementation of the property setter.
Are you certain that setting .title property is what you want? If you're in a UINavigationController you may actually want
self.navigationItem.title
instead. Setting the title is independent of source of the title, so I'd work on getting that second View Controller to display a static title you choose, and only then turn to the task of passing a title dynamically from the first VC. Your issue is likely in setting the title string, not in how you pass that string around from VC 1 to VC 2.
As obvious as it seems, the .title property of a UIViewController doesn't always actually cause a title string to be displayed… the rules for when that property is actually used get tricky.
I am assuming that "titleView" is nothing but of type NSString.
In your SecondViewController.h, synthesize your titleView property.
And NSLog it in your SecondViewController before assigning it to self.title just to check that it's getting some value we tried to assign.
Hope this helps!