获取正确的ViewController实例,以便与委托函数

时间:2017-02-10 05:40:48

标签: ios swift uiviewcontroller delegates protocols

我正在尝试创建一个委托函数,以便在事件的不同视图控制器中重新加载集合视图。

为此,我已经定义了一个协议,在类中设置委托,以及一个简单的委托函数。

protocol ReloadCollectionDelegate: class {
    func reloadCollectionViewFromDelegate()
}


class JourneyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITabBarControllerDelegate, UIScrollViewDelegate, ReloadCollectionDelegate {

    // delegate function from downloadCollectionController to relaod collection
    func reloadCollectionViewFromDelegate() {
        // simply call the reload function
        reloadCollection()
    }

在我班上将调用上述函数:

// define the delegate for use
weak var reloadJourneyDelegate: ReloadCollectionDelegate?

// reload the collection view in JourneyViewController
let JVC = self.viewController.storyboard!.instantiateViewController(withIdentifier: "JourneyViewController") as! JourneyViewController
self.reloadJourneyDelegate = JVC
self.reloadJourneyDelegate?.reloadCollectionViewFromDelegate() 

打印(JVC):

JourneyViewController: 0x7fc7f7c55bf0

print(self) - 来自JourneyViewController(viewDidLoad):

JourneyViewController: 0x7fc7f7e2db10

所以我得到了同一个视图控制器的不同实例。

我如何定义这个,所以我有正确的实例,可以修改用户界面?

由于

2 个答案:

答案 0 :(得分:1)

此行创建JourneyViewController

的新实例
self.viewController.storyboard!.instantiateViewController(withIdentifier: "JourneyViewController") as! JourneyViewController

您必须有办法回顾JourneyViewController的原始实例。通过具有指向它的属性或两个视图控制器都在层次结构中,使JourneyViewController是另一个视图控制器的父级:

class ViewControllerA: UIViewController {
    override viewDidLoad() {
        let button = UIButton(frame: CGRect(x: self.view.bounds.width/2, y: 400, width: 50, height: 50))
        button.backgroundColor = UIColor.black
        button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
        self.view.addSubview(button) 
    }

    func buttonPressed() {
        let journey = self.parent as! JourneyViewController
        journey.reloadCollection()
    }
}

答案 1 :(得分:0)

这对我有用,因为上面的答案没有:

@Bean
public CustomAuthenticationFilter customAuthenticationFilter() throws Exception {
    CustomAuthenticationFilter filter = new CustomAuthenticationFilter("/login");
    filter.setAuthenticationManager(this.authenticationManagerBean());
    filter.setAuthenticationFailureHandler(failureHandler);
    filter.setAuthenticationSuccessHandler(successHandler);
    //If don't set it here, spring will inject a compositeSessionAuthenticationStrategy bean automatically, but looks like it didn't work as expected for me
    filter.setSessionAuthenticationStrategy(compositeSessionAuthenticationStrategy());
    return filter;
}
@Bean
    public ConcurrentSessionControlAuthenticationStrategy sessionControlAuthenticationStrategy() {
        ConcurrentSessionControlAuthenticationStrategy csas = new ConcurrentSessionControlAuthenticationStrategy(sessionRegistry());
        csas.setExceptionIfMaximumExceeded(true);
        return csas;
    }

导入部分是您必须在身份检查器中添加情节提要ID。