Two Problems I'm having with UIButton and UIView - iphone

I haven't been programming on the iPhone for very long, but I'm picking it up slowly by googling problems I get. Unfortunately I haven't been able to find an answer for these.
I have started a new View-based application in Xcode 3.2.2 and immediately added the following files: myUIView.m and myUIView.h, which are subclasses of UIView. In Interface Builder, I set the subclass of the default UIView to be myUIView.
I made a button in the drawRect method.
Problem one: The title of the button only appears AFTER I click the screen, why?
Problem two: I want the button to produce the modalview - is this possible?
The code is as follow:
#import "myUIView.h"
#implementation myUIView
- (void)drawRect:(CGRect)rect {
// Drawing code
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0,0,100,100);
[button setTitle:#"butty" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
-(void)buttonPressed:(id)sender{
NSLog(#"Button pressed");
//present modal view somehow..?
}
I can't see how to post attachments, but if anyone thinks it will help I can upload the source.
Many thanks,
Andy

drawRect: is for custom drawing. You won't often need to do that. Your button won't be drawn until it gets sent drawRect:, probably in the next iteration of the run loop (ie, when you click on the view).
If you want a button on your view, either drag it there using IB, or move the code from drawRect: to viewDidLoad.
You present a modal view from a view controller by:
[self presentModalViewController:viewController animated:YES];
Do this in the action method your button sends to your view controller. This should indicate to you that you need to take a look at your application design.

Related

iPhone the beginner's application [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Short pre-introduction to a problem:
I applied to a position of iOS developer. For this position they don't require knowledge of Objective-C or iOS development. The requisites are a good experience with C/C++ and development for Linux platform.
Problem:
After the end of interview I got a task (with words that it's easy to impelement, is it?) and I can't understand whether it's so.
Interviewer has showed me an application on his iPhone that looked like:
Pressing/dragging that button leads to appearance of such bar:
He could scroll it and drag on the screen. When he had pressed the control button, bar disappeared.
I want to ask for excuse in advance, because I feel myself like a person who wants his job to be done by others. But I just want understand how I can approach to this problem. Cocoa library is a big and a new beast to me. Which classes/elements from it is it better to use to reach this aim? I programmed before with Qt, Tkinter, WinAPI (gui programming) for desktop but it doesn't help me a lot here.
I need to code something similar like the interviewer has showed me. I tried to find something in the internet similar to this, but without success.
So, I just want to know for which classes it's better to look for in Cocoa library.
And, if I failed to take something into consideration I would be glad (happy) to hear your notes.
Thanks in advance for any replies!
SOLVED
I want to thank everyone who replied and didn't leave me to sink into this unknown sea.
After a week of exploring I got what I wanted:
Source code: (I apologize, but I can't copy-paste code from virtual mac os machine)
Now I understand how too-wide and silly is my question, but anyway perhaps it helps someone to start out. Also one book helped me a lot.
There is no required understanding of Objective-C, but the interviewer wants you to create a simple application and return it to him? Interesting interview.
As for your question. The classes I see being needed here (aside the AppDelegate and a UIViewController of your choice) are:
UIButton
UIScrollView
UIImageView
The button's use here is obvious. The scrollview is again obvious. The imageview would be the icons inside the scrollview.
Allright - just to give you some guidance on what to start with and how to proceed so that you don't get lost in the great wide world of cocoa ...
And assuming that you are familiar with programming and have some decent experience in the c++ and java worlds ...
Open xcode.
Go for blank view based app. Single view application would do. Go for Automatic Reference Counting (ARC).
xcode creates a template for you with an application delegate (which you would not need for this task)
A view controller is created and a xib/nib file for an iPhone view. (ignore the ipad view if any).
Add to your view controllers so called IBAction methods. One for the top button and one for the others. An action is supposed to be called when a button is pressed (and other events which you don't need).
Properly defined you will see that action in the interface builde when editing the xib file. Add a button and connect it with this action (touch up inside would be best I think).
Compile this. When your button is pressed the action is executed. Set a breakpoint there to make sure that it got called.
Now, in this action, you either call a newly created method showScroller and hideScroller. And create those methods.
Now it gets a bit more complicated.
In showScroller you would have to ...
create a UIScrollView. (alloc it and init it)
create a number of UIButtons. (no alloc withot its init in objective-c)
set your second IBAction method as target for the buttons.
Position the buttons within the UIScrollView accordingly.
Position the UIScrollView nicely under the top button.
add the UIButtons as subview to the contentview of the Scroll view.
add the scroll view as subview to self.view (that is the underlying view, the grey thing in the interface builder).
In hideScroller you would have to ...
remove the UIScrolView from the view. For that you could either remove all subviews from self.view or you would have to keep a reference to the scroll view within your view controller in some instance variable. Pretty similar to C++ and Java which you know already.
Alternative to the scenario described above you could create all views within interface builder and pre-define the scroll view as hidden in IB (interface builder) and in showScroller and hideScroller you would just have to set its .hidden property to YES or NO. But I've got a guts feelting that with your background you should and could do this programmatically instead of hasseling with how IB connects into your code.
In the event that you really run into that trap and don't get the top most button properly connected to your code, then create that button programmatically too.
Ah, I forgot. The right place to create all those UI elements would be the viewDidLoad method of the ViewController class. There is an empty one generated already in your empty xcode template. Just enance it following the [super viewDidLoad]; call.
Have a look at this:
http://www.cocoacontrols.com/platforms/ios/controls/aurosetteview
When you press a button, the items show up and when you press again, it closes.
The source code is within the library. Use it to read up.
For your case, you need to include the scrollview as well.
Here you go:
Open XCode and create a new project. Choose iOS Empty Application template.
Go to menu, choose File, New File, Objective-C class. Name it RootViewController and choose a subclass to be UIViewController
Go to your app delegate .m file (If your project name was Test, this file is TestAppDelegate.m). At the top of the file, below the line where it says #import "TestAppDelegate.h" add another line that says #import "RootViewController.h"
In the same file there is a method named:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
at the end of the method but before return YES; statement add the following line:
self.window.rootViewController = [[RootViewController alloc] init];
Now go to your RootViewController.m file that you created in the step 2. and at the top of the file edit the interface to have this one instance variable:
#interface RootViewController ()
{
UIView *menuView;
}
#end
In the same file locate the method named - (void)viewDidLoad and add the following code in it: - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor =[UIColor blackColor];
UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
menuButton.frame = CGRectMake(100, 60, 50, 50);
[menuButton setBackgroundImage:[UIImage imageNamed:#"menu.png"] forState:UIControlStateNormal];
[menuButton addTarget:self action:#selector(menuButtonTapped:) forControlEvents:UIControlEventTouchDown];
CGRect menuFrame = CGRectMake(20, menuButton.frame.origin.y + 50, 300, 60);
menuView = [[UIView alloc] initWithFrame:menuFrame];
menuView.backgroundColor = [UIColor purpleColor];
menuView.hidden = YES;
UIButton *menuButtonA = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonA.frame = CGRectMake(5, 5, 50, 50);
[menuButtonA setTitle:#"A" forState:UIControlStateNormal];
[menuView addSubview:menuButtonA];
UIButton *menuButtonB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonB.frame = CGRectMake(65, 5, 50, 50);
[menuButtonB setTitle:#"B" forState:UIControlStateNormal];
[menuView addSubview:menuButtonB];
UIButton *menuButtonC = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonC.frame = CGRectMake(125, 5, 50, 50);
[menuButtonC setTitle:#"C" forState:UIControlStateNormal];
[menuView addSubview:menuButtonC];
UIButton *menuButtonD = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonD.frame = CGRectMake(185, 5, 50, 50);
[menuButtonD setTitle:#"D" forState:UIControlStateNormal];
[menuView addSubview:menuButtonD];
UIButton *menuButtonE = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonE.frame = CGRectMake(245, 5, 50, 50);
[menuButtonE setTitle:#"E" forState:UIControlStateNormal];
[menuView addSubview:menuButtonE];
[self.view addSubview:menuButton];
[self.view addSubview:menuView];
}
Below that method add a new method:- (void)menuButtonTapped:(id)sender
{
if (menuView.hidden)
menuView.hidden = NO;
else
menuView.hidden = YES;
}
Find some nice png image on the internet that will represent your menu button. Name it menu.png and drag and drop it in your project(inside the "file" menu on the left side)
That's it run the project ;)
just read the iphone's beginner book. you have to read some book first. first try to understand basic controls in iphone & basic manipulation in objective c
your above work is very basic.now just you are away from one step and that is read a book, you will get lots of free pdf on net

Detecting when a custom UIButton has been pressed

I have created a custom UIButton (UIButton subclass) for my application. Within this button's implementation, how would I alter the background color when it is pressed? (It should revert to it's normal background when depressed. I am currently using Core Graphics to draw a gradient background.)
I have tried branching in drawRect: (checking for self.selected and self.highlighted, but no change occurs because drawRect is not being called on the touch event.
Any suggestions or code samples are appreciated.
Try calling [self setNeedsRedraw] in your touch event.
Asker provided a solution based on this answer and the comments below.
Solution
Used:
[self addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:#selector(buttonDepressed) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel];
Where the selectors are custom functions that perform the background change.

How do I launch a modal view from a custom UITabBar?

I've built out the raised-center UITabBar from this GitHub location.
My challenge now is that I can't figure out how to create a modal view that will appear when the button is pressed.
Has anyone used the idev-recipes RaisedCenterTabBar with luck? How did you implement the modal sheet that appears there?
Alternatively, is there a different gitHub project that has a working custom tab bar with a modal sheet?
Thank you!
Here was my solution, it was BY FAR the cleanest way I found to do this... I really hope it helps, I spent hours researching the best ways.
I setup a "UITabBarController" delegate that connects directly to my tab interface built out on my storyboard.
** Don't forget to include the "tabBarController" delegate in your header file
** Notice this callback method is NOT the "didSelectViewController" but rather the "shouldSelectViewController". This method handles the request before the tab is selected and that is exactly what you want so you can STOP the request before it ever happens... This way you don't have to save the current index you are on, pass it around and all that nonsense.
Then I am simply checking what tab WILL be selected (based on the view controller's title.
** Also: this is my code, change it as needed for your code. But the principals should remain. My "performSegueWithIdentifier" is actually a manual segue connected to my tab controller that opens in a modal. This code is working brilliant for me.
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if([[viewController title] isEqualToString:#"tellvc"])
{
[self performSegueWithIdentifier:#"shareModelViewController" sender:Nil];
return NO;
}
else
{
return YES;
}
}
I have something similar in a program of mine that I'm working on and would be glad to show you how I do it. I have a couple of viewControllers in a TabBar. I create my Plus button in whichever VC I decide will appear first on the screen in ViewDidLoad.
// Create a plus button that appears on the tabBar
UIImage *plusButton = [UIImage imageNamed:#"plusbutton.png"];
UIView *tabBarView = [[self tabBarController] view];
addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setFrame:CGRectMake(127.0, 432.0, [plusButton size].width, [plusButton size].height)];
[addButton setBackgroundImage:plusButton forState:UIControlStateNormal];
[tabBarView addSubview:addButton];
[addButton addTarget:self action:#selector(scalePicker:) forControlEvents:UIControlEventTouchUpInside];
I make the button a subView of the tabBarController's view. Later on in the implementation of this VC I have a method called scalePicker: which creates and instance of one of my other VC's and presents it modally. Here is the code for that: (note: this is the method that I set as a target for the plus button in the code above)
-(void) scalePicker:(id)sender
{
// create the view scalePicker, set it's title and place it on the top of the view hierarchy
sp = [[ScalePickerVC alloc] init];
[self presentModalViewController:pickerNavController animated:YES];
}
I hope this helps you,
Good Luck!

Touch Event on image , achieved by using UIButton but show up delayed compare to UIImageView

Sorry for the messy title, I just don't know how to describe the problem in a delicate way.
I'm writing a album-like app to display a bunch of image in my scrollview and do something when a image is touched.
I followed this question : how can i detect the touch event of an UIImageView and use button with background image to handle touch event.
My original method is using NSOperation to concurrently fetch image from internet and put it io a imageview and add to my scrollview, and the speed is quite ok because each imageview shows right after each NSOperation callback.
Then I change imageview to uibutton, the strange thing is that when a NSOperation callback, that button does not show in my view. They show up at once when all the NSOperation callback is done. That makes the user experience become unacceptable.
This is my NSOperation callback function, it will pass a button that contains the image fetched from internet
- (void)imageLoaded:(UIButton*)button;
{
NSLog(#"Done");
[button addTarget:self action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
The buttons will only displa after the last "Done" appear instead of one by one, is that normal? or I messed up something?
==== update ========
I think I'm running the NSOperation on my viewcontroller. I have a imageLoadOperation class, I'll pass my viewcontroller to it
imageLoadOperation *ilo = [[imageLoadOperation alloc] initWithURLString:[NSString stringWithFormat:#"link for the image"];
[ilo setParent:self];
[queue addOperation:ilo];
[ilo release];
And in the main function of imageLoadOperation I'll do
[parentViewController performSelectorOnMainThread:#selector(imageLoaded:) withObject:button waitUntilDone:NO];
Do you mean I need to move these code to my AppDelegate instead of running in my viewcontrollor?
You can use a button of custom type over your image view instead of using button with background image, or you can use touch event on an UIImageView
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
What if instead of converting to a button you just allowed your UIImageView to handle the touch event?
To add a touch event to a UIImageView use the following format in your .m
UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(newTapMethod)];
[_imageButtonName setUserInteractionEnabled:YES];
[_imageButtonName addGestureRecognizer:newTap];
then create the newTapMethod (or what ever you name it)
-(void)newTapMethod{
//...
}
hope that helps.

How to design a Navigation-Based app for the iPhone?

I am a new iPhone developer learning Objective-C, and trying to build an iPhone app using a Navigation-Based template. The way I want this application to function is to have a button, which takes the user to the second screen after being pressed. Unfortunately, I have not seen any examples of this online, and am unsure how to do this.
I realize that I won't be using a table view, where I would select a particular row that would then take me to another screen. What view would be appropriate to have a button, and possibly a search bar on the first screen, which would then allow me to navigate over to a second screen? I would need the second screen to have a table view which would hold a list of rows retrieved from a sqlite database. What method in the RootViewController (i.e. the first screen) would I use to place the code to execute when the button is selected (i.e. fires an event)?
Not sure I understand you question correctly, but...
In RootViewController:
- (void) viewDidLoad
{
UIButton* button = [UIButton buttonWithStyle:...];
[button setFrame:CGRectMake(0, 0, 10, 10)];
[button addTarget:self action:#selector(onButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:button];
}
- (void) onButtonPressed
{
SecondViewController* controller = ...;
[[self navigationController] pushViewController:controller animated:YES];
}
The addTarget:action:forControlEvent: selector registers the 'callback' that should be called every time the button is pressed. In the callback (onButtonPressed) you just create new controller and push it into the navigation controller.
Usually you should do it in the Interface Builder. Just add IBAction and connect it to the event.