0

I have defined the width, height and position of a custom UIButton in the viewDidLoad section of my implementation file. If I were to drag and drop this button in the IB, I could ctrl and drag it to another View Controller and select PopOver.

Unless there is a way to attach an IBOutlet to this custom UIButton which I'm unaware of, as my custom button doesn't show in the Interface Builder, how do I go about calling a PopOver of a UIViewController within the action of clicking my custom UIButton in my implementation file?

This is how I defined my custom button:

self.hard1 = [UIButton buttonWithType:UIButtonTypeCustom];
[self.hard1 setFrame:CGRectMake(884, 524, 105, 60)]; // set the x,y,width and height
UIImage *buttonImage = [UIImage imageNamed:@"green.jpg"];
self.hard1.layer.cornerRadius = 10;
self.hard1.clipsToBounds = YES;
[self.hard1 addTarget: self
          action: @selector(buttonTapped:)
forControlEvents: UIControlEventTouchUpInside];
[self.hard1 setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:self.hard1];
jhilgert00
  • 5,479
  • 2
  • 38
  • 54
Louis Holley
  • 135
  • 1
  • 3
  • 10
  • Are you asking about storyboard segues? – Bryan Feb 24 '13 at 21:11
  • yes, I am, can I use those within my code? – Louis Holley Feb 24 '13 at 21:14
  • I've edited my answer to show how to call a segue programmatically, but I don't really understand what is so custom about your button. Why can't you just put it in using IB? – Bryan Feb 24 '13 at 21:18
  • because I change the colour of it under a certain condition, so unfortunately I had to code it. Thanks very much for your help, could you just clarify what I drag the segue to? – Louis Holley Feb 24 '13 at 21:21
  • Drag the segue to the view you want as a popover. Like you would do from the button, only there is no button so you have to do it from the ViewController. – Bryan Feb 24 '13 at 21:23
  • It might be easier to set up a reference outlet to this button and change the colour that way, rather than create the whole thing custom. – Bryan Feb 24 '13 at 21:24
  • where does mySegue come into it? Sorry if I'm sounding stupid I'm just struggling to grasp this. Thanks for your patience. – Louis Holley Feb 24 '13 at 21:27
  • Also I wasn't aware of any other way to change the colour of the button – Louis Holley Feb 24 '13 at 21:28

3 Answers3

0

If i understood you correctly, your custom button is a subclass of UIButton. So in IB, put a regular button in the right place, then navigate to the Identity inspector in the right-side pane, and in the class field enter your custom button class name, so that IB button becomes CustomUIButton. You work everything else as if it was regular button.

You have to have you custom class imported in the view controller's .h-file.

Hope it helps.

user1244109
  • 2,166
  • 2
  • 26
  • 24
  • My button is called hardButton1, if I enter that in the class field it just retains the same properties of a normal button, not the ones I have written in the implementation file. Thanks for the response. – Louis Holley Feb 24 '13 at 20:16
  • Ok man, then just make sure you didn't misspell methods you have rewritten, and that they are declared in the .h file. – user1244109 Feb 24 '13 at 20:22
0

Connect the action touchupinside to the method buttonTapped: via interface builder. To do so

  • Right click on the button on the Interfacebuider ,
  • In the window there will be list of actions.
  • click and drag teh touchUpInside and put it on the viewcontroller object
  • then another window shows the methods and connect it with the desired method .Thats it

Go through this tutorial

EDIT :

  [hardButton1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

self is where you defined the method buttonPressed:

EDIT 2:

From the comments i think the problem may be button image or frame.Set a background color for the image and test .Also check you set the proper imagename with extension.Put a breakpoint and test the method is hit if button is visible

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • I defined my button programatically, so it doesn't display in the Interface Builder, only when it runs the app. – Louis Holley Feb 24 '13 at 20:40
  • that's what I did, I still don't understand how to use the Interface Builder to do this when my button doesn't show up there – Louis Holley Feb 24 '13 at 20:53
  • "button doesnt show up there ? what do you meant by that? can you explain more? – Lithu T.V Feb 24 '13 at 20:56
  • where I defined the UIButton in my implementation file, I set the width, height and its position on the screen, so I see it when I run the application, but I can't see it in the Interface Builder – Louis Holley Feb 24 '13 at 21:04
  • well you have to add a button in the IB and make its custom class your button class.See implementation will not bring button in IB.We have to add it and connect it to make the compiler know this button in IB represent this Reference in Class.For that we user IBOutlets – Lithu T.V Feb 24 '13 at 21:05
  • That makes sense, thank you. I'm not entirely sure what to write in the class field though.. – Louis Holley Feb 24 '13 at 21:08
  • what is your custom class name? give it there if you are using one.Otherwise just drag in a button and connect the outlet – Lithu T.V Feb 24 '13 at 21:10
0

Something like this:

- (IBAction) buttonTapped:(id) sender
{
    [self performSegueWithIdentifier:@"mySegue" sender:self];
}

where mySegue is a popover segue that you have dragged from the containing viewController to the one you want to pop over. You create the segue and then type a name in the 'Identifier' box for the segue in IB, then you use the same name here where I've put 'mySegue'.

screen grab showing segue identifier

I've ringed the two buttons you have to press to see this display; then you just have to select the segue that you want to work on. You can select it in the picture (click on the middle of the arrow), or in the list of items in your storyboard.

Bryan
  • 11,398
  • 3
  • 53
  • 78
  • can I refer to a ViewController in the Interface Builder? Or do I have to programatically define one? – Louis Holley Feb 24 '13 at 20:39
  • You can call up a particular layout designed using IB with method `initWithNibName:` on the ViewController. I'm not sure I understand what you want. Maybe if you took a step back and described at a higher level? – Bryan Feb 24 '13 at 21:01
  • how do I change the properties of the IB like you have in that picture? – Louis Holley Feb 24 '13 at 21:57
  • I expanded the picture a bit and tried to explain in the answer. – Bryan Feb 24 '13 at 22:08
  • Sorry Bryan, thanks a lot for the help. I'm now receiving an error 'Popover segue with no anchor' – Louis Holley Feb 24 '13 at 22:12
  • Yes, that is a complication. Various people have solved that problem, e.g: http://stackoverflow.com/questions/7787119/creating-button-for-popover-view-anchor-at-run-time – Bryan Feb 24 '13 at 22:24