【问题标题】:How to check whether cancel or add button is pressed in PKAddPassesViewController如何检查 PKAddPassesViewController 中是否按下了取消或添加按钮
【发布时间】:2012-12-28 11:08:11
【问题描述】:

默认情况下,通行证加载到PKAddPassesViewController。有什么方法可以知道在视图上按下了哪个按钮。

//this method runs when user either click on the cancel or add button

-(void)addPassesViewControllerDidFinish: (PKAddPassesViewController*) controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

我想获取在PKAddPassesViewController 中按下的按钮的标题。我已尝试使用以下代码来访问标题,但我得到了null

NSLog(@"Title of button    %@",controller.navigationController.navigationItem.rightBarButtonItem.title);

【问题讨论】:

    标签: ios6 passbook


    【解决方案1】:

    据我所知没有,但您可以随时尝试检索您刚刚添加的通行证:

    - (PKPass *)passWithPassTypeIdentifier:(NSString *)identifierserialNumber:(NSString *)serialNumber;
    

    如果添加了 pass,则返回 nil,否则将返回 nil - 这有助于推断是否添加了新的 pass。

    请注意,除了添加之外,右侧按钮还可以显示“更新”(如果该通行证已经存在但您的版本有新数据),或者如果您尝试重新添加重复的通行证,则该按钮被禁用。

    【讨论】:

    • 苹果的耻辱!
    【解决方案2】:

    我使用了另一种方法来解决上述问题。 我比较的是没有。在用户单击添加或取消按钮后,存折中已经存在的具有新通过计数的通行证数量。如果通行证计数增加 这意味着通行证已添加到存折中,否则不会。

    -(void)addPassesViewControllerDidFinish: (PKAddPassesViewController*) controller{
        PKPassLibrary* passLib = [[PKPassLibrary alloc] init];
        NSArray * passArray = [passLib passes];
    
        int currentPasses=[passArray count];
        //Here prevPasses are passes already present in the Passbook.You can 
        //initialise it in -(void)viewDidLoad method
    
        if(currentPasses>prevPasses){
          NSLog(@"Pass Has Been successfully Added");    
        }else{
          NSLog(@"Cancel Button Clicked"); 
        }
     }
    

    //但是在更新同一个pass的情况下,pass count不会增加导致else部分的执行 //无论你是点击取消还是升级按钮。所以你需要为 //tracking提供一些额外的逻辑它。

    【讨论】:

      【解决方案3】:

      试试这个,

      -(void) addPassesViewControllerDidFinish:(PKAddPassesViewController *)controller {
      
          if (self.HKPass) {
              PKPassLibrary *pkLibrary = [[PKPassLibrary alloc] init];
              if ([pkLibrary containsPass:self.HKPass]) 
                      // add or update clicked
              else 
                 // Cancel Clicked   
      
          }
          [controller dismissModalViewControllerAnimated:YES];
      
      }
      

      谢谢

      【讨论】:

        【解决方案4】:

        Swift.4 版本的Karthikeyan's 答案。

        不要忘记为您的 PKAddPassesViewController 设置委托。

        func addPassesViewControllerDidFinish(_ controller: PKAddPassesViewController) {
            let passLib = PKPassLibrary()
        
            // Get your pass
            guard let pass = self.pass else { return }
        
            if passLib.containsPass(pass) {
                // Add button pressed
        
                // Show alert message for example
                let alertController = UIAlertController(title: "", message: "Successfully added to Wallet", preferredStyle: .alert)
        
                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
                    controller.dismiss(animated: true, completion: nil)
                }))
        
                controller.show(alertController, sender: nil)
        
            } else {
                // Cancel button pressed
                controller.dismiss(animated: true, completion: nil)
            }
        }
        

        【讨论】:

          【解决方案5】:

          斯威夫特 5.2

          pass的modifiedDate将在添加或再次添加时更新 您可以获取 modifiedDate 然后将其与当前日期进行比较

          let pkl:PKPassLibrary = PKPassLibrary()
          // get the pass to check from the wallet
          if let pass = pkl.pass(withPassTypeIdentifier: "pass.com.example.yourapp", serialNumber: "serialNumber"){
            // get the modified date
            if let modifiedDate = pass.value(forKey: "modifiedDate") as? Date{
              let result = modifiedDate.distance(to: Date())
              // check if the modified date is within an interval
              if result.isLess(than: 2){
                // add is pressed
              }else{
                // cancel is pressed
              }
            }
          }else{
              // cancel is pressed
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-01-25
            • 1970-01-01
            • 1970-01-01
            • 2020-01-02
            • 2015-01-14
            • 2015-10-30
            • 1970-01-01
            相关资源
            最近更新 更多