I have these two classes in my project:
#import <UIKit/UIKit.h>
#import "TableViewController.h"
#interface MainViewController : UIViewController
#end
and
#import <UIKit/UIKit.h>
#interface TableViewController : UITableViewController
#end
Within MainViewController.m, I am trying to do this:
TableViewController *tview = [[TableViewController alloc] initWithNibName:#"TableViewController"
bundle:nil];
tview.tableView.dataSource = tview;
tview.tableView.delegate = tview;
[self.view addSubview: tview.view];
However, this is crashing with an:
-[MainViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xb52dc90'
I don't understand why MainViewController is becoming the datasource/delegate for the TableViewController, when I already set the delegate/datasource in the init above. I also tried to set TableViewController's delegate/datasource to self from within its viewDidLoad, but the MainViewController still keeps acting as the delegate/datasource, regardless. I tried with the nib files hooked up and not hooked up, but no difference.
Ideally, I want the TableViewController to act as the delegate and datasource, and have its view added to MainViewController. How can I get this done?
Thank you!
Note: Neither solution worked for some reason. In the end, I recreated TableViewController as inheriting from UIViewController and set it as and things went normally then.
Where have you written the TableView delegate methods?? They should be inside your TableViewController Class and not inside the MainViewController Class.
Write the UITableView delegate methods inside the TableViewController Class.
Hope this will solve your problem...
Have you set delegate and datasource to nil in dealloc method?
tview.tableView.delegate = nil;
tview.tableView.dataSource = nil;
Related
I'm working on an app and I need to pass data between view controllers. I know this is a common question but I couldn't find an answer for my problem : I'm able to pass data from the FirstViewController (MasterViewController in my case) to the SecondViewController (SettingsViewController) but not the reverse. What happens is that I call a method from the FirstViewController in my SecondViewController.m file. This works and it logs the data. But when I quit the SecondViewController (using [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];) the data is reset.
I tried using other methods to pass data but it didn't work. I'm using this code to pass data:
MasterViewController *vc = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
[vc setPorts:SelectedPorts];
I also tried replacing [vc setPorts:SelectedPorts]; with vc.selectedCellIndexes = SelectedPorts; but the same problem occurs.
the setPorts method is declared in the FirstViewController.h file and SelectedPorts is a variable I declared in SecondViewController.m (it's not nil I checked).
Here's the setPorts: in FirstViewController.m :
- (void) setPorts:(id)selectedPorts {
selectedCellIndexes = selectedPorts;
NSLog(#"selectedCellIndexes : %#", selectedCellIndexes);
}
This logs the good value but when I log it in viewWillAppear in FirstViewController.m it's reset to the value it has before I called the method from SecondViewController.m.
Just to clarify, if I DON'T quit the SecondViewController.m, the data isn't reset.
I did read all your comments, and I really thanks you for your help. for convenience, I used a global variable.
Thanks for your help.
You have a list of ports in MasterViewController and you expect to use it in the SettingsViewController.
The MasterViewController can hold this list and SettingsViewController should have an access to it.
In SettingsViewController, have a setSelectedPort method:
#property (nonatomic, retain) id selectedPorts
- (void) setPorts:(id)selectedPorts;
The method saves the selected ports list into a property.
In MasterViewController, call the SettingsViewController and give it the list.
SettingsViewController *vc = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
[vc setSelectedPorts:yourValue];
When the list is modified inside the SettingsViewController, the list of ports contained in MasterViewController won't move even if you leave the SettingsViewController.
In secondViewController, You create protocol
#import <UIKit/UIKit.h>
#protocol sampleDelegate <NSObject>
- (void)passValue:(id)selectedPorts
#end
#interface SecondViewController : UIViewController
#property (nonatomic, strong) id <sampleDelegate> passDelegate;
#end
In viewDidLoad or wherever method as per your need, call method like this,
[self.passDelegate passValue:selectedPorts];
In FirstViewController.h,
Import the delegate <sampleDelegate>,
#import "SecondViewController.h"
#interface FirstViewController : UIViewController<SampleDelegate>
#end
In FirstViewController.m,
-(void)passValue:(id)selectedPorts
{
id receivedValues = selectedPorts;
}
and set self in your SecondViewController allocation,
SecondViewController *vc = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
vc.passDelegate = self;
There is nothing unusual in the getting result. By doing
MasterViewController *vc = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
[vc setPorts:SelectedPorts];
You are creating a new instance of MasterViewController from your SecondViewController. This is not the same from which you navigated to the SecondViewController. So you wont get the expected result. Since you are setting the ports([vc setPorts:SelectedPorts]) to the newly created instance of the Master.
Instead of creating a new instance,just hold the reference of the MasterViewController in SecondViewController in a property and assign it before moving to second VC. As a beginner I suggested this way. But using delegate is the prefferred way passing data back.
Either use delegate methods to communicate with the master VC from the modal VC, or you could do something like this if you want to retrieive some manipulated objects from the modal VC.
Set the object(s) as properties in the modal view controller's .h-file (so they are public).
Using unwind segues, in the master VC, just do this:
-(IBAction)exitModalVC:(UIStoryboardSegue*)segue
{
SomeObject *obj = ((YourModalVC*)segue.sourceViewController).someObject;
//Do what you want with obj
}
EDIT:
This will only work if you are using unwind segue (which is a neat way of dismissing modal VC when using story board)
And you are using this, which is not unwind segues:
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
You were creating a new instance of the first view controller from the 2nd view controller not accessing the same instance of the original caller. That was the reason why while you could see the logs but data were not there when you got back to the original caller - your MasterViewController.
You need to use delegate method. Check my answer for this SO.
This is problem related to object ownership.
Follow the below steps:
As per understanding you want reverse value from "SecondViewController" to "FirstViewController"
Don't create new object of FirstViewController in SecondViewController, it will not work.
Create object of "FirstViewController" in "SecondViewController.h" file.
#property (nonatomic,strong) FirstViewController *firstViewController;
When you navigate from FirstViewController to SecondViewController, please pass the "self".
e.g. SecondViewController *vc = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
vc.firstViewController = self;
If you want pass the reverse value to FirstViewController then in SecondViewController.m file.
[self.firstViewController setPorts:SelectedPorts];
And in FirstViewController.m refresh your controls with latest values.
Try above code will defiantly work as per your requirement.
I think I've done my homework here.
I want my app delegate to be the delegate for my UITabBarController.
Using IB, I've connected UITabBarController's delegate to my Application Delegate.
App Delegate Header file is:
#interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
The delegate method I'm trying to implement is:
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSLog(#"shouldSelectViewController called.");
// do some stuff with viewController
return YES;
}
My app delegate has an outlet to the UITabBarController that's connected in IB. When I do this:
NSLog(#"tab bar controller delegate is %#", self.tabBarController.delegate);
I get a good result such as tab bar controller delegate is <MyAppDelegate: 0x6e86a30>.
What am I missing?
Just write this code. Usually in viewDidLoad().
self.tabBarController.delegate = self;
If the current controller is a UITabBarController then:
self.delegate = self
Ok, found the solution. I had some old code in my RootViewController that set up this controller as the delegate. No delegate methods were implemented on the RootViewController, so it appeared as if nothing was happening. Because the RootViewController is set as delegate AFTER MyAppDelegate, the delegate was actually set to the RootViewController.
So the lesson is double-check your code to make sure some other object isn't also being set as the delegate.
I have an UIViewController with a UIToolBar at the top with 3 buttons and a UIView, when touch upInside those buttons I have to change the views that the controller has. What can I do to get my porpuse? Thanks in advance.
You probably want to use something like a UINavigationController to control the view stack and then have your button(s) call one of these methods for the Touch Up Inside action:
pushViewController:animated:
popViewControllerAnimated:
popToRootViewControllerAnimated:
popToViewController:animated:
Here is a good uinavigationcontroller-tutorial to look into.
You need to do something like this for each of the actions you set up.
In the .h file of the current viewController:
#import "OtherViewController.h"
#interface MyViewController : UIViewController
{
OtherViewController *otherViewController;
}
#property(nonatomic, retain)IBOutlet OtherViewController *otherViewController;
Then in the .m file of the current viewController you need to add the following for each IBAction (touch up inside).
At the top of the .m file add:
#synthesize otherViewController;
Then make an IBAction and put the following line of code to display the other view:
[self presentModalViewController:otherViewController animated:NO];
In your otherViewController you can dismiss itself by using:
[self dismissModalViewControllerAnimated:NO];
NOTE: The other thing you will need to do is create a UIViewController in Interface Builder for each of the views you plan to display. You need to then go into the identity inspector and set the Class as OtherViewController. You then need to link the IBOutlet to the OtherViewController as normal.
There is a YouTube video tutorial which covers all of what I have mentioned above. It's a nice simple way to get started.
UiViewController view property is the base view you are seeing. It could be set(replaced with another). SO replace the view object of ViewController with another view you created.
UIView * customView = [[[UIView alloc] initWIthFrame:viewFrame] autorelease];
[self setView:customView];
Here self represent the current viewController.
I have created a UITabBarController in my app delegate.
where each tab bar item holds a different UINavigationController that loads a custom UIViewController with a NIB (using -pushViewController:).
Inside one of the navigation controller, I load a custom UIView class with a custom NIB also.
This view is loaded multiple times inside the UIViewController.
The custom UIView has a UIButton, that on the event of touching it, I want to push a new UIViewController on the stack.
Problem is that I 'lost' the UINavigationController that holds the UIViewController.
I know I should use delegates, but haven't figured out who should which class should be the delegate.
Thanks in advance!
Neither .navigationController or .tabBarController will be available for a UIView or UIViewController that's been created but not pushed onto a stack
Either create a property on your View (or ViewController) class that is a UIViewController that is provided optionally after initialization or you could add a third argument to initWithNibName:bundle:
#interface CustomViewController : UIViewController
{
UIViewController *owner;
}
#property (nonatomic, assign) UIViewController* owner;
#end
Then in the owner ViewController:
CustomViewController *cvc = [[CustomViewController alloc] initWithNibNamed:nil bundle:nil];
cvc.owner = self;
It's too bad .parentViewController is read-only, this would be the sensible place for this.
You can get this using UIApplication class. Using this class you can find which viewController is placed at first. Here is the solution link for your problem.
I asked this question earlier with way too much code.
The ViewController initializes a UIView chain, Controller>>View>>SubView, in the ViewController. After the SubView is initialized the ViewController is set as its delegate:
aSubView.delegate = self;
NSLog(#"$#",aSubview.delegate), returns the ViewController, so I know it is set.
In the SubView, NSLog(#"$#",self.delegate),returns random crap such a hr.lproj or a file path to the Foundation framework.
It crashes when attempting to implement any of the delegates methods, since the delegate doesn't link to the ViewController but instead randomness.
This is what the SubView.h file looks like:
#import "TestDelegate.h"
#interface TestSubView : UIView {
id<TestDelegate> delegate;
}
#property (assign) id<TestDelegate> delegate;
EDIT: ViewController is initialized in the app delegate as such:
ViewController *controller = [[ViewController alloc] init];
[window addSubview:controller.view];
[controller release];
The only other thing I added to the App Delegate, over the default is an import of the ViewController header
Is it possible the view controller object is being released/dealloced between the two calls to NSLog?