আমি কীভাবে প্রোগ্রামিকভাবে একটি বেসিক ইউআইবাটন তৈরি করব?


545

আমি কীভাবে একটি প্রাথমিক UIButtonপ্রোগ্রামটি তৈরি করতে পারি ? উদাহরণস্বরূপ আমার দৃশ্যের নিয়ামকটিতে, viewDidLoadপদ্ধতিটি কার্যকর করার সময় , তিনটি UIButtonগতিশীলভাবে তৈরি করা হবে এবং এর বিন্যাস বা বৈশিষ্ট্যগুলি সেট করা আছে।


webindream.com/how-to-add-uibutton-programmatically-in-swift একটি ভাল uibutton যোগ করার জন্য টিউটোরিয়াল প্রোগ্রামেটিক্যালি হয়েছে
ফরহাদ রুবেল

সুইফট মধ্যে UIButton তৈরি করুন, stackoverflow.com/questions/24102191/...
আইওএস

উত্তর:


1187

এখানে একটি:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

ধন্যবাদ! তুমি আমাকে বোঝাতে পারো ম্যাথোদ কি?
ডোমলাও

5
বাটনউইথটাইপ: ইউআইবাটন
অ্যালিন

261
হ্যান্ডি টিপ: আপনি যদি চান না যে আপনি আপনার বোতামটি স্পর্শ করার সাথে সাথেই সক্রিয় করতে চান তবে ইউআইসিএন্ট্রোলএভেন্টটিচডাউনডের পরিবর্তে ইউআইসিএন্ট্রোলএভেন্টটচআপসাইড ব্যবহার করুন - এটি ব্যবহারকারীকে আঙুল তুলতে অপেক্ষা করবে, এটিকে টেনে এনে বাতিল করার বিকল্পটি দেবে (অ্যাপলে প্রস্তাবিত প্রস্তাবিত) হিউম্যান ইন্টারফেসের গাইডলাইন)
আয়রগুইটার

9
আমি বেশিরভাগ ক্ষেত্রে ইউআইসিএন্ট্রোলইভেন্ট টাচআপ ইনসাইড ব্যবহার করার পরামর্শ দিই না। আমি কিছু গেমস এবং সংগীত প্লেিং অ্যাপগুলিতে (ক্লিফটুনস এবং ক্লিফনোটস) ইউআইকিন্ট্রোলএভেন্টটিউচডাউন ব্যবহার করেছি যাতে স্পর্শটি তত্ক্ষণাত সঙ্গীত নোট তৈরি করে।
মাহবুডজ

45
আমার মতো লোকদের জন্য দ্রষ্টব্য যে এটির সাথে লড়াই করেছে - @ নির্বাচক (এমথোড :) একটি যুক্তি সহ একটি পদ্ধতি বোঝায়। @ সিলেক্টর (এমথোড) কোনও যুক্তি ছাড়াই এমন একটি পদ্ধতিকে বোঝায়। যদি আপনার পদ্ধতির স্বাক্ষরটি দেখতে লাগে - (অকার্যকর) এমথোড; এবং আপনি @ সিলেক্টর (aMethod :) ব্যবহার করেন, আপনার অ্যাপ্লিকেশন ক্রাশ হবে।
চাদ শুল্টজ

94
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addMyButton];   // Call add button method on view load
}

- (void)addMyButton{    // Method for creating button, with background image and other properties

    UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
    [playButton setTitle:@"Play" forState:UIControlStateNormal];
    playButton.backgroundColor = [UIColor clearColor];
    [playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
    UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
    UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
    UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
    [playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playButton];  
}

@ সাসায়ইনস: আমি কোডটি সাজিয়ে রেখেছি, আপনি ভিডিডিএলএডে অ্যাডমাইবটন পদ্ধতিটি কল করতে পারেন।
Xorsat

1
ধন্যবাদ ... তবে কেন ভিউ কন্ট্রোলার ব্যবহার করুন: ভিউডিডলড? দেখে মনে হচ্ছে এই স্টাফটি (ভিউ সেটআপ করা / বোতাম যুক্ত করা) ভিউ কন্ট্রোলারের পরিবর্তে ভিউতে করা উচিত? ধন্যবাদ
রক্কা রাগ

@ ক্র্যাকারেজে অ্যাডমাইবটন () পদ্ধতিটি জেনেরিক পদ্ধতি, আপনি ইভেন্ট থেকে এটি ব্যবহার করতে পারেন।
Xorsat

1
যেটি গ্রহণ করা হয়েছে তার চেয়ে অনেক ভাল উত্তর, কারণ এটি কোডের প্রসঙ্গটি দেখায়।
মরগান উইল্ড

মনে হচ্ছে এটি একটি ভাল ধারণা (??) দেখুন স্থানাঙ্ক আরম্ভের করতে viewDidLoad মধ্যে নেই stackoverflow.com/questions/17882199/... ???
ব্যবহারকারী 2054339

85

উদ্দেশ্য গ

UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
 [but setFrame:CGRectMake(52, 252, 215, 40)];
[but setTitle:@"Login" forState:UIControlStateNormal];
[but setExclusiveTouch:YES];

 // if you like to add backgroundImage else no need
   [but setbackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];

[self.view addSubview:but];

-(void) buttonClicked:(UIButton*)sender
 {
NSLog(@"you clicked on button %@", sender.tag);
 }

দ্রুতগতি

    let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
    myButton.setTitle("Hai Touch Me", forState: .Normal)
    myButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
    myButton.frame = CGRectMake(15, 50, 300, 500)
    myButton.addTarget(self, action: "pressedAction:", forControlEvents: .TouchUpInside)

    self.view.addSubview( myButton)


func pressedAction(sender: UIButton!) {
   // do your stuff here 
  NSLog("you clicked on button %@", sender.tag)
}

সুইফট 3 এবং তারপরে

  let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
    myButton.setTitle("Hi, Click me", for: .normal)
    myButton.setTitleColor(UIColor.blue, for: .normal)
    myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)

    myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside)
    self.view.addSubview( myButton)



func pressedAction(_ sender: UIButton) {
   // do your stuff here 
  print("you clicked on button \(sender.tag)")
}

SwiftUI

উদাহরণস্বরূপ আপনি সুইফটআইআই বিকাশকারী পোর্টাল থেকে ধাপে ধাপে প্রয়োগটি পান

import SwiftUI

struct ContentView : View {
    var body: some View {
        VStack {

            Text("Target Color Black")
             Button(action: { 
                 /* handle button action here */ })
            {
         Text("your Button Name")
          .color(.white)
                        .padding(10)
                        .background(Color.blue)
                        .cornerRadius(5)
                        .shadow(radius: 5)
                        .clipShape(RoundedRectangle(cornerRadius: 5))
     }


        }
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

সুইফট 3: exped declarationmyButton.setTitle এ ত্রুটি দেয় ("হাই, ক্লিক করুন")
এক্সকডিয়ান সোলঙ্গি

@ এক্সকোডিয়ানসোলঙ্গি - myButton.setTitle("Hi, Click")সিনট্যাক্সটি নয় কি myButton.setTitle("Hi, Click me", for: .normal)আমি আমার কোড চেষ্টা করেছি আমাকে কোনও সমস্যার মুখোমুখি করা হচ্ছে না
আনবু.কার্তিক

আপনার সংজ্ঞায়িত হিসাবে আমি এটি লিখি, এখন আমি ভিসি এবং ফাংশনের ভিতরে অন্যান্য অংশগুলিতে ধ্রুবক সংজ্ঞায়িত করেছি
এক্সকোডিয়ান সোলঙ্গি

30

আপনার নিয়ামকের দৃশ্যে প্রোগ্রামে একটি বোতাম যুক্ত করতে, নিম্নলিখিতটি ব্যবহার করুন:

-(void)viewDidLoad
{
   UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   btn.frame = CGRectMake(0, 0, 100, 50);
   [btn setTitle:@"Hello, world!" forState:UIControlStateNormal];
   [self.view addSubview:btn];
}

এর মধ্যে তিনটি যুক্ত করতে, ধুয়ে ফেলুন এবং পুনরাবৃত্তি করুন।


মনে হচ্ছে এটি একটি ভাল ধারণা (??) দেখুন স্থানাঙ্ক আরম্ভের করতে viewDidLoad মধ্যে নেই stackoverflow.com/questions/17882199/... ???
ব্যবহারকারী 2054339

3
ঠিক আছে, ২০০৯ এর চেয়ে এখন আরও ভাল পন্থা রয়েছে।
জেসন

25

এখানে আপনি গতিশীলভাবে একটি ইউআইবাটন তৈরি করতে পারেন:

//For button image
UIImage *closebtnimg = [UIImage imageNamed:@"close_btn.png"];
//Custom type button
btnclose = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//Set frame of button means position
btnclose.frame = CGRectMake(103, 257, 94, 32);
//Button with 0 border so it's shape like image shape
[btnclose.layer setBorderWidth:0];
//Set title of button
[btnclose setTitle:@"CLOSE" forState:UIControlStateNormal];
[btnclose addTarget:self action:@selector(methodname:) forControlEvents:UIControlEventTouchUpInside];
//Font size of title
btnclose.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//Set image of button
[btnclose setBackgroundImage:closebtnimg forState:UIControlStateNormal];

25

আসুন, এটি 2014! কোড ব্লক মূল্যায়ন কার্য এখনও কেন ব্যবহার হচ্ছে না, ট্রেন্ডগুলি দেখায় এটি ভবিষ্যতের!

UIButton* button = ({
    //initialize button with frame
    UIButton* button = [[UIButton alloc] initWithFrame:({
        CGRect frame = CGRectMake(10.0, 10.0, 200.0, 75.0);
        frame;
    })];
    //set button background color
    [button setBackgroundColor:({
        UIColor* color = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];
        color;
    })];
    //set button title for state
    [button setTitle:({
        NSString* string = [NSString stringWithFormat:@"title words"];
        string;
    }) forState:({
        UIControlState state = UIControlStateNormal;
        state;
    })];
    //set selector
    [button addTarget:self action:({
        SEL select = @selector(method:);
        select;
    }) forControlEvents:({
        UIControlEvents event = UIControlEventTouchUpInside;
        event;
    })];
    //return button
    button;
});
[self.view addSubview:button];

ওহো!

ওহো!

বা সঠিক ফলাফল যেমন হিসাবে সম্পাদন করা যেতে পারে:

UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 200.0, 75.0)];
[button setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
[button setTitle:@"title words" forState:UIControlStateNormal];
[button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];


8
উপরে উপরে কিছুটা হলেও এটি একটি আকর্ষণীয় নতুন বৈশিষ্ট্য।
পল সোল্ট

কোড ব্লক মূল্যায়ন এবং প্রশ্নের উত্তর দেখাতে পয়েন্টটি। এটি বিরক্তিকর হয়ে আসছে -1 এর ...
জন রিসালভাতো

1
কোড ব্লকের মূল্যায়ন কার্যনির্বাহ কেন গুরুত্বপূর্ণ এই বিষয়ে
ফাহিম পার্কার

ফাহিমপারকার, প্রশ্ন কীভাবে এবং কেন সিবিই গুরুত্বপূর্ণ তা নয় কীভাবে একটি বোতাম তৈরি করবেন question
জন রিসেলভাতো

3
আমি প্রসঙ্গটি নিয়ে মন্তব্য করব , আসুন 2014!
ফাহিম পার্কার

20

'action:@selector(aMethod:)' এই পদ্ধতি লিখুন:

- (void)aMethod:(UIButton*)button
   {
         NSLog(@"Button  clicked.");
  }

এটা আমার জন্য কাজ করে. ধন্যবাদ। কে এস।


14

উদ্দেশ্য গ

// Create the Button with RoundedRect type
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// instend of "Click Me" you can write your own message/Label
[mybutton setTitle:@"Click Me" forState:UIControlStateNormal];
// create the Rectangle Frame with specified size
mybutton.frame = CGRectMake(10, 10, 300, 140); // x,y,width,height    [self.view addSubview:mybutton];// add button to your view.

দ্রুতগতি

let button   = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
self.view.addSubview(button)

9

এই কোডটি একটি বোতাম তৈরি করতে চেষ্টা করুন এবং এটি আরও দুটি বারের জন্য বিভিন্ন স্থানাঙ্কের সাথে পুনরাবৃত্তি করুন এবং বোতামটি টিপানোর সময় পদ্ধতিটি (মাইবাটনক্লিক) বলা হয়

UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClick:)    forControlEvents:UIControlEventTouchUpInside]; 
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = @"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: editButton];

-(void) myButtonClick:(NSString *)myString{

   NSLog(@"you clicked on button %@", myString);
}

মাইস্ট্রিং কী সেট করা আছে?
ডগ নুল

এটি কেবলমাত্র অতিরিক্ত তথ্যের জন্য প্যারামিটার হিসাবে পাস করার স্ট্রিং, আপনি এই পদ্ধতিটিও এর মতো ব্যবহার করতে পারেন - (শূন্য) মাই বাটনক্লিক {এনএসএলগ (@ "আপনি বোতামে ক্লিক করেছেন"); } তবে নিশ্চিত হয়ে নিন যে পদ্ধতিটি কল করার সময় এটি অপসারণ করুন: ঠিক এই জাতীয় ব্যবহার করুন [মাই বাটন অ্যাডট্যাজেট: স্ব-ক্রিয়া: @ নির্বাচনকারী (মাইবাটনক্লিক) নিয়ন্ত্রণের জন্য: ইউআইকন্ট্রোলএভেন্টটিউচইউপিআইন্সিড];
আশোকদী

কীভাবে কেবল একটি বোতাম তৈরির পরিবর্তে একাধিক সেটিংস সেট করতে হবে তা দেখানোর জন্য +1।
ম্যাট ওয়াগনার

7

এই কোডটি দেখুন:

সুইফট 4.2

let frameimg = CGRect(x: 15, y: 46, width: 55, height: 70)
let btnTest = UIButton(type: .roundedRect)
btnTest.frame = frameimg
btnTest.tag = 11
btnTest.setTitle("Test Button", for: .normal)
btnTest.addTarget(self, action: #selector(self.buttonAction(sender:)), for: .touchUpInside)
btnTest.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12.0)
btnTest.titleLabel?.lineBreakMode = .byWordWrapping
btnTest.titleLabel?.numberOfLines = 2
btnTest.titleLabel?.textAlignment = .center
btnTest.setTitleColor(UIColor.gray, for: .normal)
btnTest.setTitleColor(UIColor.blue, for: .selected)
btnTest.showsTouchWhenHighlighted = true
view.addSubview(btnTest)

উদ্দেশ্য গ

CGRect frameimg = CGRectMake(15, 46, 55,70);
UIButton  *SelectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
SelectionButton.frame=frameimg;
SelectionButton.tag=i;
[SelectionButton setTitle:[SelectionArray objectAtIndex:0] forState:UIControlStateNormal];
[SelectionButton addTarget:self action:@selector(BtnSelected:)
                  forControlEvents:UIControlEventTouchUpInside];
[SelectionButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
SelectionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
SelectionButton.titleLabel.numberOfLines = 2;
SelectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[SelectionButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
[SelectionButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[SelectionButton setShowsTouchWhenHighlighted:YES];
[self.view addSubview:SelectionButton];

আমি আশা করি আপনি এই কোডটি আপনার জন্য কাজ করে।


6

আপনি কেবল স্রষ্টাকে উদাহরণটি একটি লুপের মধ্যে রেখে দিতে পারেন এবং আপনার ইচ্ছা হলে একটি অ্যারে থেকে গতিশীলভাবে নাম যুক্ত করতে পারেন।


হ্যাঁ আমি মাহবুডজ চেষ্টা করেছিলাম এবং একই সাথে লুপিংয়ের চেষ্টা করেছি। ধন্যবাদ
ডোমলাও

4
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 100.0, 300.0, 20.0);
[self.view addSubview:button];

4
-(UIButton *)addButton:(NSString *)title :(CGRect)frame : (SEL)selector :(UIImage *)image :(int)tag{

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = frame;
[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:title forState:UIControlStateNormal];
[btn setImage:image forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
btn.tag = tag;

return btn;

}

এবং আপনি এটিকে ভিউতে যুক্ত করতে পারেন:

[self.view addSubview:[self addButton:nil :self.view.frame :@selector(btnAction:) :[UIImage imageNamed:@"img.png"] :1]];

3
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
       action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

3

এটি তিনটি বোতাম তৈরির পাশাপাশি একটি উদাহরণ। কেবল তাদের অবস্থান সরান।

UIImage *buttonOff = [UIImage imageNamed:@"crysBallNorm.png"];
UIImage *buttonOn = [UIImage imageNamed:@"crysBallHigh.png"];

UIButton *predictButton = [UIButton alloc];
predictButton = [UIButton buttonWithType:UIButtonTypeCustom];
predictButton.frame = CGRectMake(180.0, 510.0, 120.0, 30.0);
[predictButton setBackgroundImage:buttonOff forState:UIControlStateNormal];
[predictButton setBackgroundImage:buttonOn forState:UIControlStateHighlighted];
[predictButton setTitle:@"Predict" forState:UIControlStateNormal];
[predictButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[predictButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:predictButton];

3

আপনি এই কোড দ্বারা বোতাম তৈরি করতে পারেন।

 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchDragInside];
 [btn setTitle:@"click button" forState:UIControlStateNormal];
  btn.frame = CGRectMake(50, 100, 80, 40);
  [self.view addSubview:btn];

এখানে বোতাম অ্যাকশন পদ্ধতি

 -(void)btnAction
 {
   NSLog(@"button clicked");
 }

2

সুইফট ২.০ এর জন্য:

let btnObject : UIButton  = UIButton() 
btnObject.frame = CGRect(x: 8, y: 89, width: 70, height: 22)

btnObject.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13)
btnObject.titleLabel?.textColor = UIColor.whiteColor()
btnObject.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1)
btnObject.titleLabel?.textAlignment = NSTextAlignment.Center
btnObject.addTarget(self, action: "btnbtnObjectClick:", forControlEvents: UIControlEvents.TouchUpInside)
subView.addSubview(btnObject)

2

প্রোগ্রামিয়ালি ইউআইবাটন তৈরির জন্য আমরা উদ্দেশ্য এবং সি উভয় ক্ষেত্রে তৈরি করতে পারি

সুইফট 3

let buttonSwift   = UIButton(type: UIButtonType.system) as UIButton
                      //OR 
let buttonSwift   = UIButton(type: UIButtonType.Custom) as UIButton
//Set Frame for Button
buttonSwift.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
//Set title for button
buttonSwift.setTitle("ClickMe", for: .normal)
//If you want to set color for button title
buttonSwift.setTitleColor(UIColor.white, for: .normal)
//If you want to set Background color for button
buttonSwift.backgroundColor = UIColor.black
//If you want to set tag for button
buttonSwift.tag = 0
//If you want to add or set image for button
let image = UIImage(named: "YourImageName") as UIImage?
buttonSwift.setImage(image, for: .normal)
//If you want to add or set Background image for button
buttonSwift.setBackgroundImage(image, for: .normal)
//Add action for button
buttonSwift.addTarget(self, action: #selector(actionPressMe), for:.touchUpInside)
//Add button as SubView to Super View
self.view.addSubview(buttonSwift)

ইউআইবাটন অ্যাকশন পদ্ধতি

func actionPressMe(sender: UIButton!) 
{
    NSLog("Clicked button tag is %@", sender.tag)
               OR
    print("Clicked button tag is \(sender.tag)")

    //Then do whatever you want to do here

    ........  
}

উদ্দেশ্য গ

UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeCustom];
           OR
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeSystem];
buttonObjectiveC.frame = CGRectMake(200, 100, 200, 100);
//Set title for button
[buttonObjectiveC setTitle:@"ClickMe" forState:UIControlStateNormal];
//If you want to set color for button title
[buttonObjectiveC setTitleColor:[UIColor  whiteColor] forState: UIControlStateNormal];
//If you want to set Background color for button
[buttonObjectiveC setBackgroundColor:[UIColor blackColor]];
//If you want to set tag for button
buttonSwift.tag = 0;
//If you want to add or set image for button
UIImage *image = [UIImage imageNamed:@"YourImageName"];
[buttonObjectiveC setImage:image forState:UIControlStateNormal];
//If you want to add or set Background image for button
[buttonObjectiveC setBackgroundImage:image forState:UIControlStateNormal];
//Add action for button
[buttonObjectiveC addTarget:self action:@selector(actionPressMe:)forControlEvents:UIControlEventTouchUpInside];
//Add button as SubView to Super View
[self.view addSubview:buttonObjectiveC];

ইউআইবাটন অ্যাকশন পদ্ধতি

- (void)actionPressMe:(UIButton *)sender 
{
    NSLog(@"Clicked button tag is %@",sender.tag);
    //Then do whatever you want to do here
    ..........
}

আউটপুট স্ক্রিনশট হয়

এখানে চিত্র বর্ণনা লিখুন

এখানে চিত্র বর্ণনা লিখুন


1
-(void)addStuffToView
{
    UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 20, 20)]; //(x, y, width, height of button on screen
    [aButton setTitle:@"Button" forState:UIControlStateNormal];//puts the text on the button
    aButton.titleLabel.font = somefont;//sets the font if one is already stated
    aButton.titleLabel.font = [UIFont fontWithName:@"Arial-MT" size:12];//sets the font type and size
    [aButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];//see back method below
    [aButton setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];//sets the image of the button
    [self.view addSubview:back];
}

-(void)back
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle.....]
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self addStuffToView];//adds all items built in this method to the view
}

1

সুইফট ২.২ এর জন্য (নতুন "নির্বাচক" ঘোষণার সাথে)।

let btn = UIButton(type: UIButtonType.System) as UIButton
btn.frame = CGRectMake(0, 0, 100, 20) // set any frame you want
btn.setTitle("MyAction", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(MyClass.myAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)


func myAction(sender:UIButton!){
  // Some action 
}

1

আপনি এটি আপনার ViewDidLoadপদ্ধতিতে প্রয়োগ করতে পারেন :

continuebtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, view1.frame.size.width-20, 40)];
    [continuebtn setBackgroundColor:[UIColor grayColor]];
    [continuebtn setTitle:@"Continue" forState:UIControlStateNormal];
    continuebtn.layer.cornerRadius = 10;
    continuebtn.layer.borderWidth =1.0;
    continuebtn.layer.borderColor = [UIColor blackColor].CGColor;
    [continuebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [continuebtn addTarget:self action:@selector(continuetonext) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:continuebtn];

কোথায় continuetonext:

-(void)continuetonext
{
    GeneratePasswordVC *u = [[GeneratePasswordVC alloc]init];
    [self.navigationController pushViewController:u animated:YES];
}

1

সুইফট 3 হিসাবে, সিনট্যাক্সে বেশ কয়েকটি পরিবর্তন করা হয়েছে।

আপনি এখানে সুইফট 3 হিসাবে একটি বেসিক বোতামটি তৈরি করতে যাবেন:

    let button = UIButton(type: UIButtonType.system) as UIButton
    button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
    button.backgroundColor = UIColor.green
    button.setTitle("Example Button", for: UIControlState.normal)
    self.view.addSubview(button)

সুইফ্টের পূর্ববর্তী সংস্করণগুলি থেকে এখানে পরিবর্তনগুলি করা হয়েছে:

let button = UIButton(type: UIButtonType.System) as UIButton

// system no longer capitalised

button.frame = CGRectMake(100, 100, 100, 50)

// CGRectMake has been removed as of Swift 3

button.backgroundColor = UIColor.greenColor()

// greenColor replaced with green

button.setTitle("Example Button", forState: UIControlState.Normal)

// normal is no longer capitalised

self.view.addSubview(button)

0

চেষ্টা করে দেখুন ....

UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
finalPriceBtn.frame=CGRectMake(260, 25, 45, 15);
[finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal];
finalPriceBtn.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f  alpha:1];
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
[imageView addSubview:finalPriceBtn];

আশা করি আমি সাহায্য করেছি।


0
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
                 action:@selector(aMethod:)
       forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:@"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f  alpha:1];
 [custombutton setImage:[UIImage imageNamed:@"hh.png"] forState:UIControlStateNormal];
 [view addSubview:custombutton];

0

এটা চেষ্টা কর:

প্রথমে আপনার ভিউকন্ট্রোলারের .h ফাইলটিতে এটি লিখুন

UIButton *btn;

এখন এটি আপনার ভিউকন্ট্রোলারদের ভিডডিডলডের .m ফাইলে লিখুন।

btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 20, 30, 30)];
[btn setBackgroundColor:[UIColor orangeColor]];
[btn setTitle: @"My Button" forState:UIControlStateNormal];
[btn setTitleColor: [UIColor blueVolor] forState:UIControlStateNormal];
[btn.layer setBorderWidth:1.0f];
[btn.layer setBorderColor:[UIColor BlueVolor].CGColor];
//adding action programatically
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

আপনার ভিউ কন্ট্রোলারের .m ফাইলে এটি বাইরের ভিউডিডলড পদ্ধতিটি লিখুন

- (IBAction)btnClicked:(id)sender
{
  //Write a code you want to execute on buttons click event
}

0

জন্য সুইফট 3 (এমনকি খাটো কোড)

let button = UIButton(type: UIButtonType.custom)
button.frame = CGRect(x: 0, y: 0, width: 200.0, height: 40.0)
button.addTarget(nil, action: #selector(tapButton(_:)), for: UIControlEvents.touchUpInside)
button.tintColor = UIColor.white
button.backgroundColor = UIColor.red
button.setBackgroundImage(UIImage(named: "ImageName"), for: UIControlState.normal)
button.setTitle("MyTitle", for: UIControlState.normal)
button.isEnabled = true

func tapButton(sender: UIButton) {

}

0

সুইফট 3 সংস্করণ হওয়া উচিত

let myButton:UIButton = {
            let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
            myButton.setTitle("Hai Touch Me", for: .normal)
            myButton.setTitleColor(UIColor.blue, for: .normal)
            myButton.frame = CGRect(x: 20, y: 20, width: 100, height: 40)

            myButton.addTarget(self, action: #selector(ViewController.pressedAction(_:)), for: .touchUpInside)
            self.view.addSubview(myButton)

            return myButton

        }()

0
UIButton *buttonName = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[buttonName addTarget:self 
    action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[buttonName setTitle:@"Show View" forState:UIControlStateNormal];
.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view
addSubview:buttonName];

0

ইন সুইফট 5 এবং Xcode 10.2

মূলত আমাদের দুটি ধরণের বোতাম রয়েছে।

1) সিস্টেম টাইপ বোতাম

2) কাস্টম টাইপ বোতাম (কাস্টম টাইপ বোতামে আমরা বোতামের জন্য পটভূমি চিত্র সেট করতে পারি)

এবং এই দুটি ধরণের বোতামের কয়েকটি নিয়ন্ত্রণ রাষ্ট্র রয়েছে https://developer.apple.com/docamentation/uikit/uicontrol/state

গুরুত্বপূর্ণ রাষ্ট্রগুলি হল

1) সাধারণ অবস্থা

2) নির্বাচিত রাষ্ট্র

3) হাইলাইট রাষ্ট্র

4) অক্ষম রাষ্ট্র ইত্যাদি ...

//For system type button
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50)
//  button.backgroundColor = .blue
button.setTitle("Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13.0)
button.titleLabel?.textAlignment = .center//Text alighment center
button.titleLabel?.numberOfLines = 0//To display multiple lines in UIButton
button.titleLabel?.lineBreakMode = .byWordWrapping//By word wrapping
button.tag = 1//To assign tag value
button.btnProperties()//Call UIButton properties from extension function
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)

//For custom type button (add image to your button)
let button2 = UIButton(type: .custom)
button2.frame = CGRect(x: 100, y: 400, width: 100, height: 50)
//        button2.backgroundColor = .blue
button2.setImage(UIImage.init(named: "img.png"), for: .normal)
button2.tag = 2
button2.btnProperties()//Call UIButton properties from extension function
button2.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button2)

@objc func buttonClicked(sender:UIButton) {
    print("Button \(sender.tag) clicked")
}

//You can add UIButton properties using extension
extension UIButton {
    func btnProperties() {
        layer.cornerRadius = 10//Set button corner radious
        clipsToBounds = true
        backgroundColor = .blue//Set background colour
        //titleLabel?.textAlignment = .center//add properties like this
    }
}

-1
UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btnname setTitle:@"Click Me" forState:UIControlStateNormal];

btnname.frame = CGRectMake(10, 10, 100, 140);

[self.view addSubview:btnname];
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.