ইউআইএলআর্টএ্যাকশনের জন্য রচনা হ্যান্ডলার


104

আমি UIAlertViewব্যবহারকারীর কাছে একটি উপস্থাপন করছি এবং হ্যান্ডলারটি কীভাবে লিখব তা আমি বুঝতে পারি না। এটি আমার চেষ্টা:

let alert = UIAlertController(title: "Title",
                            message: "Message",
                     preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "Okay",
                              style: UIAlertActionStyle.Default,
                            handler: {self in println("Foo")})

আমি এক্সকোডে একগুচ্ছ ইস্যু পেয়েছি।

নথি বলছে convenience init(title title: String!, style style: UIAlertActionStyle, handler handler: ((UIAlertAction!) -> Void)!)

পুরো ব্লক / ক্লোজারগুলি এই মুহুর্তে আমার মাথার উপরে কিছুটা। কোন পরামর্শ অনেক প্রশংসা করা হয়।

উত্তর:


165

আপনার হ্যান্ডলারের মধ্যে স্ব পরিবর্তে, রাখুন (সতর্কতা: UIAlertAction!)। এটি আপনার কোডটিকে দেখতে দেখতে দেবে

    alert.addAction(UIAlertAction(title: "Okay",
                          style: UIAlertActionStyle.Default,
                        handler: {(alert: UIAlertAction!) in println("Foo")}))

এটি হ'ল সুইফটে হ্যান্ডলারের সংজ্ঞা দেওয়ার সঠিক উপায়।

ব্রায়ান নীচের দিকে নির্দেশ করেছেন, এই হ্যান্ডলারগুলি সংজ্ঞায়নের আরও সহজ উপায় রয়েছে। তাঁর পদ্ধতিগুলি ব্যবহার করে বইটিতে আলোচনা করা হয়েছে, ক্লোজার্স শিরোনামে বিভাগটি দেখুন


9
{alert in println("Foo")}, {_ in println("Foo")}এবং {println("Foo")}এছাড়াও কাজ করা উচিত।
ব্রায়ান নিকেল

7
@ ব্রায়ান নিকেল: তৃতীয়টি কাজ করে না কারণ আপনার পক্ষে যুক্তি কার্যটি পরিচালনা করতে হবে। তবে এ ছাড়া আপনাকে ইউআইএলআর্টএ্যাকশনস্টাইল.ডিফাল্টটি দ্রুতগতিতে লিখতে হবে না। .ডাফল্টও কাজ করে।
বেন

মনে রাখবেন যে আপনি যদি "লেট ফু = ইউআইএলআর্ট্যাকশন (...) ব্যবহার করেন তবে আপনি ইউআইএলআর্ট্যাকশনের পরে দীর্ঘ বন্ধ হওয়া কী হতে পারে তা অনুসরণ করতে ট্রেলিং ক্লোজার সিনট্যাক্সটি ব্যবহার করতে পারেন - এটি দেখতে খুব সুন্দর দেখাচ্ছে
ডেভিড এইচ

1
এটি লেখার জন্য এখানে একটি দুর্দান্ত উপায়:alert.addAction(UIAlertAction(title: "Okay", style: .default) { _ in println("Foo") })
হ্যারিস

74

কার্যগুলি সুইফটে প্রথম শ্রেণীর অবজেক্ট। সুতরাং আপনি যদি কোনও ক্লোজর ব্যবহার করতে না চান, আপনি কেবল উপযুক্ত স্বাক্ষর সহ একটি ফাংশন সংজ্ঞায়িত করতে পারেন এবং তারপরে এটি handlerআর্গুমেন্ট হিসাবে পাস করতে পারেন । পালন:

func someHandler(alert: UIAlertAction!) {
    // Do something...
}

alert.addAction(UIAlertAction(title: "Okay",
                              style: UIAlertActionStyle.Default,
                              handler: someHandler))

উদ্দেশ্য-সি-তে এই হ্যান্ডলার ফাংশনটি কীভাবে দেখা উচিত?
andilabs

1
কার্যাবলী হয় সুইফট মধ্যে বন্ধ :) যা আমি যদিও বেশ শান্ত ছিল। দস্তাবেজগুলি দেখুন: বিকাশকারী
অ্যাপ্লিকেশন

17

আপনি সুইফট 2 ব্যবহার করে এটি সাধারণ হিসাবে এটি করতে পারেন:

let alertController = UIAlertController(title: "iOScreator", message:
        "Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
        self.pressed()
}))

func pressed()
{
    print("you pressed")
}

    **or**


let alertController = UIAlertController(title: "iOScreator", message:
        "Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
      print("pressed")
 }))

উপরের সমস্ত উত্তরগুলি সঠিক just


11

ধরে নেওয়া যাক আপনি প্রধান শিরোনাম, দুটি ক্রিয়া (সংরক্ষণ এবং বাতিল) এবং বোতামটি বাতিল করে একটি ইউআইএলআর্ট্যাকশন চান:

let actionSheetController = UIAlertController (title: "My Action Title", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)

    //Add Cancel-Action
    actionSheetController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

    //Add Save-Action
    actionSheetController.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
        print("handle Save action...")
    }))

    //Add Discard-Action
    actionSheetController.addAction(UIAlertAction(title: "Discard", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
        print("handle Discard action ...")
    }))

    //present actionSheetController
    presentViewController(actionSheetController, animated: true, completion: nil)

এটি সুইফট 2 (এক্সকোড সংস্করণ 7.0 বিটা 3) এর জন্য কাজ করে


7

সুইট ৩.০-তে সিনট্যাক্স পরিবর্তন

alert.addAction(UIAlertAction(title: "Okay",
                style: .default,
                handler: { _ in print("Foo") } ))

7

সুইফ্ট 4 এ:

let alert=UIAlertController(title:"someAlert", message: "someMessage", preferredStyle:UIAlertControllerStyle.alert )

alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: {
        _ in print("FOO ")
}))

present(alert, animated: true, completion: nil)

4

এইভাবে আমি এক্সকোড 7.3.1 দিয়ে এটি করব

// create function
func sayhi(){
  print("hello")
}

// বোতামটি তৈরি করুন

let sayinghi = UIAlertAction(title: "More", style: UIAlertActionStyle.Default, handler:  { action in
            self.sayhi()})

// সতর্কতা নিয়ন্ত্রণে বোতাম যুক্ত করা

myAlert.addAction(sayhi);

// পুরো কোডটি, এই কোডটি 2 টি বোতাম যুক্ত করবে

  @IBAction func sayhi(sender: AnyObject) {
        let myAlert = UIAlertController(title: "Alert", message:"sup", preferredStyle: UIAlertControllerStyle.Alert);
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:nil)

        let sayhi = UIAlertAction(title: "say hi", style: UIAlertActionStyle.Default, handler:  { action in
            self.sayhi()})

        // this action can add to more button
        myAlert.addAction(okAction);
        myAlert.addAction(sayhi);

        self.presentViewController(myAlert, animated: true, completion: nil)
    }

    func sayhi(){
        // move to tabbarcontroller
     print("hello")
    }

4

এক্সকোড 9 এ পরীক্ষিত সতর্কতা তৈরি করুন

let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: self.finishAlert))
self.present(alert, animated: true, completion: nil)

এবং ফাংশন

func finishAlert(alert: UIAlertAction!)
{
}

2
  1. সুইফটে

    let alertController = UIAlertController(title:"Title", message: "Message", preferredStyle:.alert)
    
    let Action = UIAlertAction.init(title: "Ok", style: .default) { (UIAlertAction) in
        // Write Your code Here
    }
    
    alertController.addAction(Action)
    self.present(alertController, animated: true, completion: nil)
  2. উদ্দেশ্য সি

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *OK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
    {
    }];
    
    [alertController addAction:OK];
    
    [self presentViewController:alertController animated:YES completion:nil];
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.