我怎样才能访问 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];
    }
}
有帮助吗?

解决方案

我这样做的方法是在创建源时传递对 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(...);
}

其他提示

您在 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 

希望再次提供帮助...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top