I have a popover view that I added a PFQueryTableViewController too, as a childviewController. I have a button on the popover that, when pressed, should reload the tableview in the PFQueryTableViewController.
Here is the code in the buttonPressed method that should do this:
NotificationsTableViewController *note = (NotificationsTableViewController *)self.childViewControllers[0];
[note loadObjects];
[note.tableView reloadData];
The table is not reloaded, though, and the only way I can get it to reload is by instantiating a whole new controller and adding it to the popover, which is not something I want to end up doing.
Here is some more code:
//NotificationsPopoverController
#class NotificationsTableViewController;
#protocol PopDelegate <NSObject>
-(void) changedQue;
#end
#interface NotificationsPopoverController : PDPopoverController
#property (nonatomic, assign) id<PopDelegate> myDelegate;
#property NotificationsTableViewController *noti;
When my button is pressed I call [self.myDelegate changedQue];
//NotificationsTableViewController.h
#interface NotificationsTableViewController : PFQueryTableViewController <PopDelegate>
//NotificationsTableViewController.m
-(void)changedQue
{
NSLog(#"Did it work?");
[self.tableView reloadData];
[self loadObjects];
}
The log statement is not printed for some reason, not sure why...
I haven't understand the flow completely yet. I think somewhere you will have to add self.note.myDelegate = self;. Hm... I think your have implemented delegate wrongly. Can you paste more complete code on both the view controller.
As per Jacob, add self.myDelegate = self.noti; right after the child view noti solves the problem.
Related
So I have a TableViewController, when certain cell pressed it goes to my DetailViewController to display more details about the pressed cell.
On this DetailViewController, I would like to add a button and when this button gets pressed a "CHECKMARK" will add to its cell from TableViewController and goes back to TableViewController. How would you do that?
I already added an IBAction for my button but I don't know how I would implement that yet..
Please speak in layman's term as much as possible because I'm not that yet very familiar with iOS Programming. Thank you so much.
You can use protocol for message transfer.
In your detailViewController create protocol,
#protocol yourDetailViewControllerDelegate <NSObject>
- (void)yourDetailViewController:(YourDetailViewController *)controller didSelectObject:(YourObject *)object;
#end
and create delegate object,
#property (nonatomic, weak) id<yourDetailViewControllerDelegate> delegate;
Then in your tableViewController implement the delegate method,
- (void)yourDetailViewController:(YourDetailViewController *)controller didSelectObject:(YourObject *)object {
// code for add check mark
}
In your button action,
- (void)buttonAction {
// Call the delegate method
[delegate yourDetailViewController:self didSelectObject:changedObject];
}
I'm calling a modal viewcontoller from a number of locations and when i close it i'd like to find out what view it is on top of so i can call a update function if it is this custom list i've made.
I'm wondering how i might call a method in the ViewController under the modal view.
Right now i've set up a delegate, but it doesnt seem to call the method that i've set up.
Please see code.
ViewController.h
#interface PICTSharePictViewController : PICTBaseShareViewController <PICTConnModalViewControllerDelegate>
.m
-(void)viewDidLoad{
PICTConnModalViewController *cmodal = [self.storyboard instantiateViewControllerWithIdentifier:#"connModal"];
cmodal.pictDelegate = self;
}
-(void)checkSwitches:(PICTConnModalViewController*)sender{
NSLog(#"-----Check-----");
}
And ModalView
.h
#class PICTConnModalViewController;
#protocol PICTConnModalViewControllerDelegate
-(void)checkSwitches:(PICTConnModalViewController*)sender;
#end
#interface PICTConnModalViewController : PICTBaseViewController {
__weak id <PICTConnModalViewControllerDelegate> sliderDelegate;
}
#property (nonatomic, weak) id <PICTConnModalViewControllerDelegate> pictDelegate;
.m
-(void)viewDidLoad{
[pictDelegate checkSwitches:self];
}
I get no errors or any warnings. Can anyone tell me what I'm doing wrong?
You can access the View Controller that presented the modal View Controller by using the -[UIViewController presentingViewController] method on the modal/presented VC.
Xcode 4.6.1 iOS 6 using storyboards
My problem is this
I have a UITableView with dynamic prototype cells on a UIView in a UIViewController (that is itself embedded in a navigation controller) and I want to segue from one specific cell to another view
(Before anyone suggests I should just be using a UITableViewController , I do have other things on the UIView, so i'm set up this way for a reason.)
Now i'm not sure how to go about creating the segue
If I drag from the prototype UITableViewCell to create a segue , all the generated cells automatically call the the segue - when i need only one to do so. This is normal behaviour and I would get around this if i was using a UITableViewController by creating the segue by dragging from UITableViewController and calling [self performSegueWithIdentifier:.... From my didSelectRowAtIndexPathMethod so only the specific cell I want to perform this segue triggers it.
I don't have a UITableViewController in this case - just my UITableView on a UIView that is part of a UIViewController subclass
I've been playing around and I have just discovered that i cannot drag from the UITableView - doesn't let you do that, so that was a deadend.
My only choice that seemed left to me was to drag from the UIViewController
So i tried that and of course XCode throws up an error on the perform segue line telling me i have ... No visible interface for 'LocationTV' declares the selector performSegueWithIdentifier. LocationTv being my tableview subclass.
What is the correct way to attempt to call the new view in this situation
Thank
Simon
First of all segues can be use only between UIViewControllers. So in case you want to perform a segue between two views that are on the same view controller, that's impossible.
But if you want to perform a segue between two view controllers and the segue should be trigger by an action from one view (inside first view controller) well that's possible.
So in your case, if I understand the question, you want to perform a segue when the first cell of a UITableView that's inside of a custom UIView is tapped. The easiest approach would be to create a delegate on your custom UIView that will be implemented by your UIViewController that contains the custom UIView when the delegate method is called you should perform the segue, here is a short example:
YourCustomView.h
#protocol YourCustomViewDelegate <NSObject>
-(void)pleasePerformSegueRightNow;
#end
#interface YourCustomView : UIView {
UITableView *theTableView; //Maybe this is a IBOutlet
}
#property(weak, nonatomic) id<YourCustomViewDelegate>delegate;
YourCustomview.m
#implementation YourCustomview
# synthesise delegate;
//make sure that your table view delegate/data source are set properly
//other methods here maybe
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0) { //or any other row if you want
if([self.delegate respondsToSelector:#selector(pleasePerformSegueRightNow)]) {
[self.delegate pleasePerformSegueRightNow];
}
}
}
YourTableViewController.h
#interface YourTableViewController : UIViewController <YourCustomViewDelegate> {
//instance variables, outlets and other stuff here
}
YourTableViewController.m
#implementation YourTableViewController
-(void)viewDidLoad {
[super viewDidLoad];
YourCustomView *customView = alloc init....
customView.delegate = self;
}
-(void)pleasePerformSegue {
[self performSegueWithIdentifier:#"YourSegueIdentifier"];
}
You can create any methods to your delegate or you can customise the behaviour, this is just a simple example of how you can do it.
My Solution
I ended up using a delegation pattern
I made a segue dragging from the my UIViewController - specifically dragging from the viewController icon (the orange circle with a white square in it - from the name bar thats under the view in the storyboard - although you could also drag from the sidebar ) to the view that i wanted to segue to.
I needed to trigger this segue from a table view cell on a table view.
TableView Bit
So i declared a protocol in my tableview header file - which is called LocationTV.h - as follows
#protocol LocationTVSegueProtocol <NSObject>
-(void) makeItSegue:(id)sender;
#end
Below that I declare a property to hold my delegate
#property (nonatomic, strong) id<LocationTVSegueProtocol> makeSegueDelegate;
To actually trigger the segue i called the makeItSegueMethod on my makeSequeDelegate in my didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
DLog(#"selected row %d",indexPath.row);
case dLocation:
{
if(indexPath.row == 2){
[_makeSegueDelegate makeItSegue:self];
} else if (indexPath.row == 7){
UIViewController Bit
and set up my UIViewController (named MultiTableHoldingVC) as implementing that protocol
#interface MultiTableHoldingView : UIViewController
<EnviroTVProtocol,LocationTVSegueProtocol> {
}
Below that i declared the protocol method in the list of my classes methods (although i'm not sure that is necessary as the compiler should know about the method as the decalration of implementing a protocol is essentially a promise to implement this method)
-(void) makeItSegue:(id)sender;
And then over in the implementation file of my UIViewController i wrote the method which essentially just calls preformSegueWithIdentifier
-(void) makeItSegue:(id)sender{
[self performSegueWithIdentifier:#"ChooseCountryNow"
sender:sender];
}
And to link it all together,as in the header file I had declared my instance of the tableView as follows
#property (strong, nonatomic) IBOutlet LocationTV *dsLocationTV;
I had to set that tables views delegate property to be self - which I did in my UIViewControllers -(void)ViewDidLoad method
_dsLocationTV.makeSegueDelegate = self;
It all seems a bit of a kludge calling a method to call a method and allprog suggestion is simpler (I cant for the life of me work out why it threw up errors for me) but this works just fine . Thanks to both allprog and danypata for their suggestions.
Hope this is helpful to someone out there
performSegueWithIdentifier: is a method of the UIViewController class. You cannot call it on a UITableView instance. Make your view controller implement the UITableViewDelegate protocol and set it as the delegate for the UITableView.
Another option is that you don't use segues. In the same delegate method do:
OtherViewController ov = [[OtherViewController alloc] init<<some initializer>>];
// Or in case of storyboard:
OtherViewController ov = [self.storyboard instantiateViewControllerWithIdentifier:#"ovidentifier"];
// push view controller
[self.navigationController pushViewController:ov animated:YES];
If the delegate object is different from the view controller, then the easiest solution is to add a weak property to the delegate's class that keeps a reference to the viewController, like this:
#property (weak) UIViewController *viewController;
and set it up in the viewDidLoad of the viewController
- (void) viewDidLoad {
self.tableView1.viewController = self;
}
Make sure that the tableView1 property is declared like this:
#property (IBACTION) (weak) SpecialTableView *tableView1;
Sometimes using the storyboard is more painful than writing the code yourself.
I'm trying to set the delegate for my custom protocol that has one required method allowing me to pass an array of objects back in the hierarchy of two UITableViewControllers. My delegate continues to return nil. Due to this, my required method is never called.
I'm wondering if the datasource and delegate implementations with my UITableViewControllers is causing a conflict. Also, perhaps ARC is getting in the way when declaring the delegate?
It should be noted that both UITableViewControllers were built using Storyboard and are navigated using segues within a UINavigationController (not sure if this may be causing issues or not).
The nav is --> AlarmViewController --> AlarmDetailsViewController. I create an Alarm object in my AlarmDetailsViewController that contains all the details for an alarm, place it into an array and I want to pass that array back to my AlarmViewController to be displayed in a custom cell in the table.
NOTE: I want to use the Delegate pattern here. I'm not interested in solutions that invoke NSNotifications or use my AppDelegate class.
AlarmDetailsViewController.h
#import "Alarm.h"
#protocol PassAlarmArray <NSObject>
#required
-(void) passAlarmsArray:(NSMutableArray *)theAlarmsArray;
#end
#interface AlarmDetailsViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
//.....
id <PassAlarmArray> passAlarmsArrayDelegate;
}
#property (nonatomic, retain) id <PassAlarmArray> passAlarmsArrayDelegate;
#end
AlarmDetailsViewController.m
#import "AlarmDetailsViewController.h"
#interface AlarmDetailsViewController ()
#end
#implementation AlarmDetailsViewController
#synthesize passAlarmsArrayDelegate;
-(void) viewWillDisappear:(BOOL)animated
{
NSLog(#"delegate = %#", self.passAlarmsArrayDelegate); // This prints nil
[[self passAlarmsArrayDelegate] passAlarmsArray:alarmsArray];
}
//....
#end
AlarmViewController.h
#interface AlarmViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, PassAlarmArray>
{
//...
AlarmDetailsViewController *alarmDetailsViewController;
}
#property (nonatomic, retain) AlarmDetailsViewController *alarmDetailsViewController;
#end
AlarmViewController.m
#import "AlarmViewController.h"
#import "AlarmDetailsViewController.h"
#import "AlarmTableViewCell.h"
#import "Alarm.h"
#interface AlarmViewController ()
#end
#implementation AlarmViewController
#synthesize alarmDetailsViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// This is where I'm attempting to set the delegate
alarmDetailsViewController = [[AlarmDetailsViewController alloc]init];
[alarmDetailsViewController setPassAlarmsArrayDelegate:self];
}
//....
//My #required protocol method which never gets called since my delegate is nil
-(void) passAlarmsArray:(NSMutableArray *)theAlarmsArray
{
alarmsTableArray = theAlarmsArray;
NSLog(#"alarmsTableArray contains: %#", alarmsTableArray); // Never gets called due to delegate being nil
NSLog(#"theAlarmsArray contains: %#", theAlarmsArray); // Never gets called due to delegate being nil
}
#end
I've attempted to set the delegate in a method that fires when a button is pressed in AlarmViewController (as opposed to the viewDidLoad method) but that does not work either.
I'm assuming I've got a logic flow error somewhere here . . . but nearly 2 days of hunting and rebuilds haven't uncovered it. Ugh.
You're setting your delegate in the wrong place, and on a different instance of the controller than the one you will get when you do the segue. You should set the delegate in the prepareForSegue method if you're pushing AlarmDetailsViewController from AlarmViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
AlarmDetailsViewController *alarm = segue.destinationViewController;
alarm.passAlarmsArrayDelegate = self;
}
You really need to understand the life cycle of view controllers, how and when they're instantiated, and when they go away. This is the very heart of iOS programming, and Apple has extensive documentation on it. Reading up on segues would also be very useful. A segue (other then an unwind segue) always instantiates a new instance of the destination controller. So, when your segue is performed, whether directly from a button, or in code, a new (different from the one you alloc init'd directly) details controller is instantiated. Before that segue is performed, prepareForSegue: is called, and that's when you have access to the one about to be created. That's the place to set a delegate or pass any information on to the destination view controller.
Did you try replace (nonatomic, retain) with (nonatomic, strong) since you are using ARC?
Auto-synthesized properties like your alarmDetailsViewController property have backing ivars prefixed with underscores, e.g. _alarmDetailsViewController. Your alarmDetailsViewController ivar (the alarmDetailsViewController declared inside the #interface ... {} block in AlarmViewController.h) is different from the backing ivar of your alarmDetailsViewController property.
Just delete your alarmDetailsViewController ivar and use the #property, preferably through self.alarmDetailsViewController.
Here is my question: I have two UITableViewController that we're going to call. OriginalTableViewController and SecondTableViewController.
The SecondTableViewController is populated by an NSMutableArray and a UISegmentedControl where the users can navigate through a bunch of data and select multiple rows.
What I want to do is to enable the users to select multiple row, click on save button in the navigation bar and then on OK to dismiss the view and go back to OriginalTableViewController which has to be populated by the selected rows of the SecondTableViewController.
I don't know exactly how to proceed since I started to learn how to code like 4 months ago. Should I use delegation? Or anything else? I would appreciate any help.
For your problem, delegation would be the best choice. You define a protocol in SecondTableViewController and implement that protocol in OriginalTableViewController. When the Save button is pressed, the second table notifies the original table with selected data, and the original can pop/dismiss the second and reloads its table.
In SecondTableViewController.h, define the protocol:
#protocol SecondDelegate <NSObject>
#required
- (void) didSelectRows:(NSArray *)rows;
#end
#interface SecondTableViewController : UITableViewController
#property (retain) id<SecondDelegate> delegate;
#end
In OriginalTableViewController, implement the protocol:
.h:
#interface OriginalTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, SecondDelegate>
.m:
- (void) didSelectRows:(NSArray *)rows {
// Update the model with selected data and reload. Also pops/dismisses second table.
}
And set the delegate property just before pushing/presenting SecondTableViewController:
SecondTableViewController *second = nil; // instantiate the vc some how
second.delegate = self;
In SecondTableViewController.m, implement the save method:
- (void) save {
NSMutableArray *array = [NSMutableArray array];
for (NSIndexPath *indexPath in [self.tableView indexPathsForSelectedRows]) {
// Populate array with selected objects.
}
[self.delegate didSelectRows:array];
}
Hope this helps.