is it Possible to pass parameter in UIView Class - objective-c

I ve got a view controller . when i pass an extra object I m getting this error #No visible interface in the below function .could u help me out.below is the code.where i am passing an object of type photo ..
ItemImageView *itemImage = [[ItemImageView alloc]initWithFrame:CGRectMake(currentPhotoPositionX,0,pageButtonWidth,pageButtonHeight) andPhoto:photo];
#ItemImageview
- (id)initWithFrame:(CGRect)frame :andPhoto:(Photo *)photo
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//Create Activity Indicator
activityIndicator= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(225, 115, 30, 30)];
[activityIndicator setBackgroundColor:[UIColor clearColor]];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[self addSubview:activityIndicator];
//Create Label Comments
lblComments = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 100.0f, 60.0f, 43.0f)];
[lblComments setText:#"Comments"];
[lblComments setBackgroundColor:[UIColor clearColor]];
[self addSubview:lblComments];
//Create Label Likes
lblLikes = [[UILabel alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 60.0f, 43.0f)];
[lblLikes setText:#"Likes"];
[lblLikes setBackgroundColor:[UIColor clearColor]];
[self addSubview:lblLikes];
//Create Item Button
btnItem = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnItem addTarget:self
action:#selector(:)
forControlEvents:UIControlEventTouchUpInside];
[btnItem setBackgroundImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
btnItem.frame = CGRectMake(0.0f, 0.0f, 144.0f, 143.0f);
[self addSubview:btnItem];
}
return self;
}

Change it into:
- (id)initWithFrame:(CGRect)frame andPhoto:(Photo *)photo
And also remember to add it to your .h file.

Related

NavigationBar alpha value changed automatically after dismissing view controller

Weird thing
navigationbar'alpha value changes after dismissing view controller
Step
Click button that presents a new viewcontroller
Set navigation bar alpha to 0.0 when the button is clicked
Dismiss viewcontroller which I presented right before.
Result [NG]
navigationbar'alpha value is set to 1.0
Expected result
navigationbar's alpha should be 0.0 as expected
Below sample is pretty much easy
ViewController class
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"%s : %f", __func__, self.navigationController.navigationBar.alpha);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.backgroundColor = [UIColor orangeColor];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *clickMe = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
[clickMe setTitle:#"clickme" forState:UIControlStateNormal];
[clickMe setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clickMe addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickMe];
}
- (void)btnClicked {
self.navigationController.navigationBar.alpha = 0.0f;
second *sec = [[second alloc] init];
[self presentViewController:sec animated:YES completion:nil];
}
second class
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.navigationBar.backgroundColor = [UIColor orangeColor];
self.view.backgroundColor = [UIColor yellowColor];
UIButton *getBack = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
[getBack setTitle:#"getBack" forState:UIControlStateNormal];
[getBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[getBack addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:getBack];
}
- (void)btnClicked {
[self dismissViewControllerAnimated:YES completion:nil];
}
I really would like to know what happened in this..
If someone knows this, let me know.. Thank you in advance.

Can't see a new subview

All I am trying to do is add a new view to the content view of my NSWindow instance. When I do the following I do not see the new view (which should be black and take up the entire window). What am I doing wrong?
(done in response to a button click)
NSRect frameRect = [self.window frame];
frameRect.origin = NSZeroPoint;
NSView *view = [[NSView alloc] initWithFrame:frameRect];
view.wantsLayer = YES;
view.layer.backgroundColor = [NSColor blackColor].CGColor;
[self.window.contentView addSubview:view];
I've set up a simple project with a push button inside a ViewController and got a warning for accessing self.window. When using self.view.windowthe warning went away and your provided code works as expected.
Updated code
NSRect frameRect = [self.view.window frame];
frameRect.origin = NSZeroPoint;
NSView *view = [[NSView alloc] initWithFrame:frameRect];
view.wantsLayer = YES;
view.layer.backgroundColor = [NSColor blackColor].CGColor;
[self.view.window.contentView addSubview:view];
Update
Assuming that you're using an instance of WindowController where you're adding a button programmatically, your code works as expected.
#implementation WindowController
- (void)windowDidLoad
{
[super windowDidLoad];
CGRect buttonRect = CGRectMake(self.window.frame.size.width / 2 - 50,
self.window.frame.size.height / 2,
100,
50);
NSButton *button = [[NSButton alloc] initWithFrame:NSRectFromCGRect(buttonRect)];
[button setTitle: #"Click me!"];
[button setTarget:self];
[button setAction:#selector(buttonPressed)];
[self.window.contentView addSubview:button];
}
- (void)buttonPressed
{
NSRect frameRect = [self.window frame];
frameRect.origin = NSZeroPoint;
NSView *view = [[NSView alloc] initWithFrame:frameRect];
view.wantsLayer = YES;
view.layer.backgroundColor = [NSColor blackColor].CGColor;
[self.window.contentView addSubview:view];
}
An instance of NSViewController ain't got a property of window - only NSWindowController has one.

UIButton as a subview in MKAnnotationView doesn't work

I have added a button to my annotation view, and it appears correctly. When the button is tapped, I want a call to be made, but when I tap the button, it doesn't respond (the button doesn't even highlight). It seems that the button doesn't receive the touch event, but I don't know why.
Here is my code in customAnnotationView.m
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
NSLog(#"initWithAnnotation start");
//initialze the view
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil)
{
CGRect frame = self.frame;
frame.size = CGSizeMake(60.0, 85.0);
self.frame = frame;
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(30.0, 42.0);
}
//this has nothing to do with my question
self.title=[[[NSString alloc] init] autorelease];
self.description=[[[NSString alloc] init] autorelease];
self.phoneNumber=[[[NSString alloc] init] autorelease];
customAnnotation * tempCust = (customAnnotation *)annotation;
self.title=annotation.title;
self.description=tempCust.description;
self.phoneNumber=tempCust.phoneNumber;
//here is the question button
self.userInteractionEnabled=YES;
self.exclusiveTouch=NO;
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Call" forState:UIControlStateNormal];
//decide the button's position
CGRect rect=button.frame;
rect.origin.x=15;
rect.origin.y=47;
rect.size.width=40;
rect.size.height=20;
button.frame=rect;
[self addSubview:button];
[button becomeFirstResponder];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
NSLog(#"initWithAnnotation finished");
return self;
}
//action method
-(void)buttonClicked:(id)sender{
NSString *callNumber=[[[NSString alloc] initWithFormat:#"tel://%#",self.phoneNumber] autorelease];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:callNumber]];
}
Why doesn't the button work?
self.rightCalloutAccessoryView = button;
Use the above instead of [self addSubview:button];
Hope this help's you

Create UIView programmatically without Interface Builder

I try to create a view without IB and make it separated with its controller. The view is MediaPlayerView contains two buttons and the controller is MediaPlayerViewController. I called the view in controller and adding a Movie Player view in it. The buttons is showing but the movie view just dark screen. What's wrong with my code?
Here is my code:
MediaPlayerView.m
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIButton *audioFileButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *shortSoundButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[audioFileButton setFrame:CGRectMake(0, 40, 150, 40)];
[shortSoundButton setFrame:CGRectMake(0, 100, 200, 40)];
[audioFileButton setTitle:#"Play Audio File" forState:UIControlStateNormal];
[shortSoundButton setTitle:#"Play Short Sound" forState:UIControlStateNormal];
_viewController = [[MediaPlayerViewController alloc] init];
[audioFileButton addTarget:[self viewController]
action:#selector(playAudioFile:)
forControlEvents:UIControlEventTouchUpInside];
[shortSoundButton addTarget:[self viewController]
action:#selector(playShortSound:)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:audioFileButton];
[self addSubview:shortSoundButton];
}
return self;
}
In MediaPlayerViewController.m, here is my loadView and viewDidLoad:
loadView:
MediaPlayerView *mPlayer = [[MediaPlayerView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[mPlayer setBackgroundColor:[UIColor clearColor]];
[self setView:mPlayer];
[mPlayer release];
viewDidLoad:
[super viewDidLoad];
float halfHeight = [[self view] bounds].size.height / 2.0;
float width = [[self view] bounds].size.width;
[[[self moviePlayer] view] setFrame:CGRectMake(0, halfHeight, width, halfHeight)];
[[self view] addSubview:[[self moviePlayer] view]];
Forgive for my bad writing, cause I am a newbie either in here, Objective-C or english.

How does autoresizing mask work?

I've found a lot of information on autoresizing but none has been able to solve my problem. I have a ViewController (RAViewController) who calls a view in its loadView method like so:
- (void)loadView
{
// Set Navigation Bar Title
self.navigationItem.title = #"RA";
// Add Background view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Add RAView
RAView *rAView = [[RAView alloc] initWithFrame:self.view.frame Controller:self];
[self.view rAView];
}
The view it calls (RAView) looks like this:
- (id)initWithFrame:(CGRect)frame Controller:(RAViewController *)Controller
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.autoresizesSubviews = YES;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
controller = Controller;
// Draw Architecture View
[self drawArchitectureView];
}
return self;
}
-(void)drawArchitectureView {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50);
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:#"Overview" forState:UIControlStateNormal];
[self addSubview:button];
}
For some reason I can't get autoresizing mask to resize the button width when the device is landscape. Any help is greatly appreciated.
If you want it to keep the same height, but just stay fully centered, 10.0 from the left and right, try:
-(void)drawArchitectureView {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50);
button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:#"Overview" forState:UIControlStateNormal];
[self addSubview:button];
}