UIButton's won't click after building in a NSObject - objective-c

I am currently building a Profile Object that can return a view with all the information of the user. I want to be able to reuse the code in many different places so I am trying to build it in an NSObject but when I add it as a subview of my view, I cannot click the buttons. The buttons are created with a rectangle and then have a UILabel put on top of them. If I copy the cody where I am trying to put it, it works. I also tried creating a delegate but that did nothing either.
EDIT
UIView *profile = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 175.0)];
UIView *votesView = [[UIView alloc] initWithFrame:CGRectMake(60.0, 125.0, 60.0, 50.0)];
UILabel *votesLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 0.0, 50.0, 40.0)];
votesView.backgroundColor = [UIColor lightGrayColor];
votesLabel.text = votes;
votesLabel.textAlignment = UITextAlignmentCenter;
votesLabel.backgroundColor = [UIColor clearColor];
votesLabel.tag = 2;
[votesView addSubview:votesLabel];
[profile addSubview:votesView];

Why are you subclassing an NSObject, you can subclass an UIView and implement all your views that are to be included,
Also, I am not able to see how you are adding a button, I can see just labels in your code.
But would suggest that you need to mention action and target, where for target you can add your class where it is called
[btn addTarget:<class where you will add UIView returned by your class> action:<SEL in your class where you will add UIView> forControlEvents:<UIControlEvents>]

Related

How Do I dim/disable the current view?

Is it possible to dim the current view in cocoa?On a certain action I want to make the screen a little darker and prevent the user from interacting with the screen.
From the idea given by #Lord Zsolt, the same can be implemented in OS X as:
Add the following code when you want to darken your window.
NSView *transparentBlackView = [[NSView alloc] initWithFrame:[[yourwindow contentView] frame]];
CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:CGColorCreateGenericRGB(0.0, 0.0, 0.0, 0.4)]; //RGB plus Alpha Channel
[transparentBlackView setWantsLayer:YES];
[transparentBlackView setLayer:viewLayer];
[[yourwindow contentView] addSubview:transparentBlackView];
Also don't forget to add QuartzCore.framework to your project. And import it in your class using #import<QuartzCore/QuartzCore.h>
Edit: I've only now noticed, it's OSX, not iOS. Though the idea should still be the same.
Disable user interaction:
[self.view setUserIntractionEnabled:NO];
Then add a UIView with black color, alpha like 0.2 above it.
UIView *transparentBlackView = [[UIView alloc] initWithFrame:self.view.frame]
[transparentBlackView setBackgroundColor:[UIColor blackColor];
[transparentBlackView setAlpha:0.2];
[self.view addSubview:transparentBlackView];
Then you can write a custom method, and use performSelector:afterDelay to remove transparentBlackView.

Adding new view into another subview

I am learning iOS. I m using Xcode 4.3.2 , and i have created a button, and in the callback, i have used the following code.
1) Created single view application
2) creating a new view -> newV
3) creating a button but;
4) adding but as a subview of newV.
5) adding newV as a subview of main view.
-(IBAction) submitButtonPressed:(id)sender
{
NSLog (#" Submit Button is pressed ");
UIView *newV = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
newV.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
UIButton *but = [[UIButton alloc] init];
[but setTitle:#"SubView" forState:UIControlStateNormal];
[newV addSubview:but];
[self.view addSubview:newV];
}
But the problem i get is, only the view viewV is shown, but button is not shown. How to resolve this issue ?
When you created the button, you didn't provide a frame. Try using:
UIButton* but = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
When you created the button, it was of zero size, so you couldn't see it.
Try adding the button after adding in the view newV.

Custom navbar titleView won't clear previous titles before loading new ones

So I created a custom UINavigationItem category to be able to make a custom titleview for my navbar, but everytime I push/pop a view, it simply adds the new title without getting rid of the old one causing the title to just be a jumble of letters. Here's the relevant code:
#implementation UINavigationItem (CustomNavigationItem)
-(UIView *)titleView
{
[self setTitleView:nil];
UILabel *newTitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 32)];
newTitleView.center = CGPointMake(160, 22);
newTitleView.backgroundColor = [UIColor clearColor];
newTitleView.textColor = [UIColor whiteColor];
newTitleView.textAlignment = UITextAlignmentCenter;
newTitleView.text = self.title;
newTitleView.textAlignment = UITextAlignmentCenter;
return newTitleView;
}
#end
You have to remove the old uilabel from its superview, by setting to nil it doesn't do that. That's why you are messing the letters on screen. I also do not think you are getting a recursion, because you are caling the setter, but I maybe wrong.
A quick thing you could is to assign a tag to your newest created view.
[[self.view viewWithTag:YourCustomEnumTag] removeFromSuperView];
// create your view....
textView.tag=YourEnumCustomTag;

UITableViewCell custom selectedBackgroundView background is transparent

I have the following code that creates a UIView that I assign to my UITableViewCell's selectedBackgroundView property. Everything works as expected, with the exception of the subview's background, which is transparent.
I use the same code to create a custom view that I assign to backgroundView, and that works fine.
What is causing that subview to be transparent for selectedBackgroundView, and how can I avoid that?
- (UIView*) makeSelectedBackgroundView
{
// dimensions only for relative layout
CGRect containerFrame = CGRectMake(0, 0, 320, 40);
UIView* containerView = [[UIView alloc] initWithFrame:containerFrame];
containerView.autoresizesSubviews = YES;
// dimensions only for relative layout
CGRect subframe = CGRectMake(5, 5, 310, 30);
UIView* subview = [[UIView alloc] initWithFrame:subframe];
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
subview.backgroundColor = [UIColor redColor];
subview.layer.cornerRadius = 5;
subview.layer.borderWidth = 2;
subview.layer.borderColor = [UIColor greenColor].CGColor;
[containerView addSubview:subview];
return containerView;
}
As we can see from name of ivar selectedBackgroundView, this background shown by cell when it was selected.
I've to reload few methods (– setSelected:animated: and – setHighlighted:animated:) of UITableViewCell subclass to reset background color of subviews back to their values. Look's like UIKit do some magic in this template methods (iterating over all UIView subclasses and set their background to clearColor)
This code might be helpful for you:
UIImageView *cellImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0,
0,
cell.frame.size.width,
cell.frame.size.height
)];
cellImageView.contentMode = UIViewContentModeScaleAspectFit;
// normal background view
[cellImageView setImage:[UIImage imageNamed:#"*<ImageName>*"]];
[cell addSubview:cellImageView];
[cell sendSubviewToBack:cellImageView];
[cellImageView release], cellImageView = nil;
Here cell is an object of custom UITableViewCell.
Also you can set backgroundColor property.
I would try to set the alpha for both containerView and subView to 1.0
[containerView setAlpha:1.0];
...
[subview setAlpha:1.0];
this should make your controls totally opaque.
You could also create some images for the background and use that images in state of creating 2 views. Let's say you create 2 image (normalBackground.png and selectedBackground.png) and then set this images as cell background. Here is a nice tutorial.
Try setOpaque:YES on your views.
In the end, I ended up subclassing UITableViewCell which contained a custom view object, and that worked.

UINavigationBar custom title being hidden by background

I am adding a custom UIImageView to my UINavigationBar using this code:
UIImageView *background = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
[background setImage:[UIImage imageNamed:#"nav_background.png"]];
[background setTag:kTagForReference];
[self.navigationController.navigationBar insertSubview:background atIndex:0];
I then add a custom title using this code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
...
[self.navigationItem setTitleView:label];
[label release];
But on some of my pushed views the title is being hidden (or isn't visible). I can't seem to push more than two views onto the stack without the title disappearing.
I've tried to force my UIImageView background to the back of the bar but that still doesn't help. I've printed the subviews of the UINavigationBar and I can see that the UILabel is there, I just can't see it!
Any help would be greatly appreciated.
If you can run the dumpWindows() function, the output would show the view hierarchy so you can see what view is covering it up.
You most likely want to use a UIToolBar instead of a NavigationBar. Then you can add the subviews to the UIToolbar.
Bring the UILabel to the front most view.
[self.view bringSubviewToFront: label];