Adding a NSButton as a subview over IKImageFlowView - macos

I am using IKImageFlowView in my application and need to add a nsbutton over it,
I am using the code as given below,
In my .h file
IBOutlet id browserView;
in nib added a custom view and changed its class as myImageFlowView as I am subclassing from IKImageFlowView as
#interface myImageFlowView : IKImageFlowView
and in my appController class I am trying to add button to browserView with following code snippet,
NSRect initialFrame = NSMakeRect(20.0, 50.0, 100.0, 100.0);
NSButton *myBtn = [[NSButton alloc] init];
[myBtn setFrame:initialFrame];
[myBtn setBordered:NO];
[myBtn setAutoresizesSubviews:TRUE];
[myBtn.cell setImageScaling:NSImageScaleAxesIndependently];
[myBtn setImage:[NSImage imageNamed:#"AppleColor.png"]];
[browserView addSubview:myBtn];
I am not able to add this button as a subview to IKImageFlowView,I can add button to NSView.
What I am doing wrong.Please help.

Related

How do I recieve the current value of a slider in code? in cocoa

i have draged vertical slider in my ui and lable for to show the slider value when i move the slider.
i have written following ibaction method.but it is not working. could you please help me in solving the issue.
-(IBAction)sliderval:(id)sender{
NSSlider *slider=[[NSSlider alloc]init];
[slider setMinValue:50];
[slider setMaxValue:100];
[slider setTarget:self];
[slider setAction:#selector(sliderDidMove:)];
[_textField setValue:slider];
}
Drag and drop UISlider and UILabel to view in xib. connect IBOutlet to interface variables and IBAction with event 'Value Changed'.
#interface ViewController ()
{
__weak IBOutlet UISlider *slider;
__weak IBOutlet UILabel *sliderValueLbl;
}
#end
- (IBAction)sliderMoved:(id)sender {
sliderValueLbl.text = [NSString stringWithFormat:#"%f",slider.value];
}

How to make a NSTextField added to a NSView Draggable

Let me elaborate the steps i am doing
Create Mac 10.9 Project
Drag a CustomView (Let's call it myView) to the .xib
Drag a NSButton to the .xib but out side the CustomView
Now programatically (using a other class) i add a NSTextField to the CustomView when the button is clicked by this code
NSTextField *textField = [[NSTextField alloc] init];
[textField setBezeled:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setFont:[NSFont fontWithName:#"Arial" size:20]];
[textField setStringValue:#"Hello World!"];
int textWidth = textField.fittingSize.width;
int textHeight = textField.fittingSize.height;
[textField setFrame:NSMakeRect(0, 0, textWidth, textHeight)];
[myView addSubview:textField];
Now i see that the TestField is added to myView
All i want is the user can drag the textField movable inside myView
i added the following code
- (void)mouseDown:(NSEvent *)event{
NSLog(#"Hi");
}
But the NSLog is not getting shown
How do i make the NSTextField draggable?
Note: Because mouse-moved events occur so frequently that they can
quickly flood the event-dispatch machinery, an NSWindow object by
default does not receive them from the global NSApplication object.
However, you can specifically request these events by sending the
NSWindow object an setAcceptsMouseMovedEvents: message with an
argument of YES.
From Apple Docset

UIButton event is not firing, even if I connect it through xib

I am creating a calculator game, in which I have to popup a UIAlertView, I customized the UIAlertView and put a UITableView programatically into it, When the user tap any of its cell I show a UIView on the top of UITableView, this view has a button, when I clicked that button the event does not fires, I have also installed it manually, but it doesn't work, previously the attachment of event with button is generating an error, for that I uninstalled the event in dealloc, and fix the problem, but after it still the event is not firing.
i was have case such your and i have to add it pragmatically
in .h declare
UIButton *btn;
in .m add it to the UIView
UIView *TempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
if (btn) {
[btn removeFromSuperview];
// [btn release]; // for not using arc
}
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10, 10, 25, 25)];
[btn addTarget:self action:#selector(ClickMe:) forControlEvents:UIControlEventTouchUpInside];
[TempView addSubview:btn];
[self.view addSubview:TempView];
and the action is
-(void)ClickMe:(id)sender{
//here is an action
}
it's working for me

How to add custom NSView to Window

I know how to do this in iOS but cannot figure it how to it in Cocoa.
I want to capture keyboard events and I think I need to override the acceptsFirstResponder method to accomplish that (keyDown method being triggered). So I created a class extending NSCustomView and tried to add it in the main Window but I just cannot understand how to do it. So far I added a Custom View to the main View then tried to add it programmatically like:
TestView *view = [[TestView alloc] init];
[[_window contentView] addSubview:view];
but this is not working. So how can I do this?
To see if the view has been added to a window, you can override the view's viewDidMoveToWindow method and log the value of [self window] to check (if it's nil then the view has been removed from a window):
- (void)viewDidMoveToWindow
{
NSLog(#"window=%p", [self window]);
[super viewDidMoveToWindow];
}
You should be subclassing NSView, not NSCustomView, and initWithFrame is the designated initializer for NSView, not init.
Try:
TestView *view = [[TestView alloc] initWithFrame:NSMakeRect(0, 0, 100, 200)];
[[_window contentView] addSubview:view];

How can I add a UIView as a subview of UIViewController when a button clicked?

In my app I need to add a UIView dynamically whenever user taps on a button in my main UIViewController. How can I do this?
Create a new method in your view controller with this signature: -(IBAction) buttonTapped:(id)sender. Save your file. Go to Interface Builder and connect your button to this method (control-click and drag from your button to the view controller [probably your File's owner] and select the -buttonTapped method).
Then implement the method:
-(IBAction) buttonTapped:(id)sender {
// create a new UIView
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,10,100,100)];
// do something, e.g. set the background color to red
newView.backgroundColor = [UIColor redColor];
// add the new view as a subview to an existing one (e.g. self.view)
[self.view addSubview:newView];
// release the newView as -addSubview: will retain it
[newView release];
}

Resources