如何在UITableViewSource下访问NavigationController.PushViewController?

时间:2014-04-11 01:35:44

标签: c# ios xamarin

如何在NavigationController课程中访问UITableviewSource

在行选择中,我想导航到另一个UIController

这是我的代码,

public class RootTableSource : UITableViewSource
{
    IList<VendorDetails> tableItems;
    string cellIdentifier = "UIViewController";

    ReportsList reportList;
    AddNewReport addnewReport;

    public RootTableSource()
    {
    }

    public RootTableSource (IEnumerable<VendorDetails> items)
    {
        tableItems = items.ToList (); 
    }

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        tableView.DeselectRow (indexPath, true); 

        // Redirect to another UIController....
    }

    public VendorDetails GetItem (int id)
    {
        return tableItems [id];
    }
}

2 个答案:

答案 0 :(得分:1)

你有AppDelegate中的实例UINavigationController吗?

像这样......

@property (strong, nonatomic) UINavigationController *navigationViewController;

然后您应该可以从AppDelegate委托

访问UINavigationController
#import "AppDelegate.h"

@implementation yourClassName

-(void)functionName{

    AppDelegate *appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

    appdelegate.navigationViewController;  //your UINavigationController

    [appdelegate.navigationViewController pushViewController:yourUIViewController animated:YES];

}

@end

祝愿帮助〜

编辑:

抱歉,我从未使用过Xamarin

所以我认为这是一种不好的实施方式......

但看起来很有效

AppDelegates.cs

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{

    UIWindow window;
    HomeScreen home;
    public static UINavigationController navigation;
    //set navigation public and static      

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        window = new UIWindow (UIScreen.MainScreen.Bounds);
        home = new HomeScreen ();
        navigation = new UINavigationController(home);
        window.RootViewController =  navigation;
        window.MakeKeyAndVisible ();
        return true;
    }
}

yourClassName.cs

AppDelegate.navigation
//access navigation 

再次希望得到帮助......

答案 1 :(得分:0)

我这样做的方法是在创建Source时传递对TableViewController的引用。然后,Source可以使用此引用来访问它的父级NavigationController。

UITableViewController _parent;

public RootTableSource (IEnumerable<VendorDetails> items, UITableViewController parent)
{
    tableItems = items.ToList (); 
    _parent = parent;
}


public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
    tableView.DeselectRow (indexPath, true); 

    _parent.NavigationController.PushViewController(...);
}